douchangmian0305
douchangmian0305
2016-09-02 19:18
浏览 20
已采纳

在没有最后一个分隔符的Wordpress中显示标签

<?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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhucha3999
    dongzhucha3999 2016-09-09 19:04
    已采纳

    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; ?>
    
    点赞 评论

相关推荐