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 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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题