Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
    if($unlinkTags)
        {
        foreach($gotTags as $tag) {echo $tag->name . ', ';}
        }
    //they want anchors, easy peasy, use get tags
    else
        {
        echo the_tags('', ', ', '');
        }
    echo '</span></div>';
}
share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.