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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号