WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm building a function in theme options to give the site owner more control over the post's "Tags" collection that I display at the bottom of each post page. Specifically, whether to add anchor text to them or not.

However, since none of the available functions: the_tags(), get_the_tags(), etc will create an unanchored list with separators, I'm left to roll my own method (unless, as I'm hoping, I'm missing something)

In the 1st part of the code branch below, once we determine the post has tags and the user has chosen to display them without links, I'm manually creating the tag list and adding a comma separator. How can I remove the separator from the last tag or from a single tag when that condition exists? (or better, is there a core method that will do it for me?)

function my_get_tags(){
    $unlinkTags = get_option('my_unlinkTags');
    $gotTags = get_the_tags();
    if(!$gotTags) return;

    //we got tags, lets create a wrapper
    echo '<div><span class="tags">';

    //no anchors and a separator? have to roll our own
        foreach($gotTags as $tag) {echo $tag->name . ', ';}
    //they want anchors, easy peasy, use get tags
        echo the_tags('', ', ', '');
    echo '</span></div>';
share|improve this question
up vote 3 down vote accepted

Try this:

$output = array();

foreach($gotTags as $tag)
    $output[] = $tag->name;

echo implode(',', $output);
share|improve this answer
+1 Yep. That's it. Thanks Rarst – Scott B Feb 9 '11 at 16:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.