• Hi, how do I remove the comma after the last tag? I’ve been searching the forums and internet in general and so far I’ve gathered that it has something to do with rtrim, however after much trial and error I don’t know how to apply it specifically to my code (below):

    <div id="tags">
    <p><?php
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->name . ' ';
      }
    }
    ?></p>
    </div>

    Any assistance is much appreciated, thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • to state the obvious – there is no comma in your code.

    if you want to have a comma, you could try:

    <div id="tags">
    <p><?php
    $posttags = get_the_tags(); $sep = '';
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $sep.$tag->name; $sep = ', ';
      }
    }
    ?></p>
    </div>
    Thread Starter marieclairehill

    (@marieclairehill)

    Thanks so much! That worked perfectly. (just to note I pasted the code before I had inserted the comma and space, which was as below, but yours solved my comma-at-the-end issue:)

    <div id="tags">
    <p><?php
    $posttags = get_the_tags();
    if ($posttags) {
      foreach($posttags as $tag) {
        echo $tag->name . ', ';
      }
    }
    ?></p>
    </div>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove comma after last tag’ is closed to new replies.