views:

57

answers:

3

I've created a custom foreach output that helps give me a tag id per post. I'm using commas to separate each tag. However, the last tag outputs a comma too, like this:

kittens, dogs, parrots, (<-- last comma)

How should I go about revising the foreach output so that the last comma is removed so it displays like this:

kittens, dogs, parrots

Here's the code:

<?php
$posttags = get_the_tags();
if ($posttags) {
    foreach($posttags as $tag) {
        echo '<a href="';
        echo bloginfo(url);
        echo '/?tag=' . $tag->slug . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>, ';
    }
}
?>
A: 

Try something like this?

<?php
$posttags = get_the_tags();
if ($posttags) {
    $loop = 1; // *
    foreach($posttags as $tag) {
        echo '<a href="';
        echo bloginfo(url);
        if ($loop<count($posttags)) $endline = ', '; else $endline = ''; // *
        $loop++ // *
        echo '/?tag=' . $tag->slug . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>' . $endline;
    }
}
?>

edit or

<?php
$posttags = get_the_tags();
if ($posttags) {
    $tagstr = '';
    foreach($posttags as $tag) {
        $tagstr .= '<a href="';
        $tagstr .= bloginfo(url);
        $tagstr .= '/?tag=' . $tag->slug . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>';
    }
    $tagstr = substr($tagstr , 0, -2);
    echo $tagstr ;
}
?>
Robert
It's the longer code of all the suggestions, but it's the one I understand. Thanks!
Marc P
+4  A: 

implode is your friend, if you don't want to be Shlemiel the painter.

$posttags = get_the_tags();
if ($posttags) {
   $tagstrings = array();
   foreach($posttags as $tag) {
      $tagstrings[] = '<a href="' . get_tag_link($tag->term_id) . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>';
   }
   echo implode(', ', $tagstrings);
}

// For an extra touch, use this function instead of `implode` to a better formatted string
// It will return "A, B and C" instead of "A, B, C"
function array_to_string($array, $glue = ', ', $final_glue = ' and ') {
    if (1 == count($array)) {
        return $array[0];
    }
    $last_item = array_pop($array);
    return implode($glue, $array) . $final_glue . $last_item;
}
Jan Fabry
This outputs the the blog's url several times before it gives the tag information correctly. Still a good function to know! Thank you!
Marc P
@bookmark: I updated my example to use [`get_tag_link`](http://codex.wordpress.org/Function_Reference/get_tag_link) instead of constructing the link myself, this will always use the correct format. And I added a "bonus" function that I used before, to get nicer output.
Jan Fabry
Ooh hot, and bookmarked! Thanks!
Marc P
A: 

You could do it with rtrim.

<?php
$posttags = get_the_tags();
if ($posttags) {
    foreach($posttags as $tag) {
        $output.='<a href="';
        $output.= bloginfo(url);
        $output.= '/?tag=' . $tag->slug . '" class="tag-link-' . $tag->term_id . '">' . $tag->name . '</a>, ';
    }
    echo rtrim($output, ', ');
}
?>
dragosplesca