kostek00 kostek00 - 2 months ago 7
PHP Question

Show tags in Wordpress without last separator

<?php $tags = get_the_tags();
if( $tags ) :
echo '<p class="taxonomy"><span class="tag-title">'.__('Tags: ', 'warp').'</span>';
foreach( $tags as $tag ) {
echo '<span class="'. $tag->slug .'"><a href="'. get_tag_link($tag->term_id) .'">'. $tag->name .'</a></span>';
}
echo '</p>';
endif; ?>


I want to use this code to show tags on pages. One function I want to use needs get_the_tags() to make it work as I wanted. I got suggested to use implode() to get rid of last separator from tag list. I don't know PHP good enough to implement it into this piece of code. The best result I got was:
Tags: tag | | tag | | and so on
. I know there are answers to similar questions but none of them worked with this code.

Any ideas how to make it work?

Answer

With big help from one user it's finally done. Hiding last separator with this code looks like that:

<?php $tags = get_the_tags();
if( $tags ) :
    echo '<p class="taxonomy"><span class="tag-title">'.__('Tags: ', 'warp').'</span>';
    foreach( $tags as $tag ) {
        $names[] = '<span class="'. $tag->slug .'"><a href="'. get_tag_link($tag->term_id) .'">'. $tag->name .'</a></span>';
    }
echo implode(' | ', $names ) .'</p>';
endif; ?>
Comments