【问题标题】:List all the tags in a WordPress blog列出 WordPress 博客中的所有标签
【发布时间】:2016-09-25 09:07:34
【问题描述】:

我需要在 WordPress 博客中列出至少 1 篇已发布帖子的所有标签。

该列表是关于完整博客的,与当前帖子无关。

你会使用什么代码?

【问题讨论】:

标签: php wordpress tags


【解决方案1】:
<ul class="tag-manually">    
<?php
    $tags = get_tags('post_tag'); //taxonomy=post_tag
    
    if ( $tags ) :
        foreach ( $tags as $tag ) : ?>
            <li class="tag-item-manually"><a class="tag" href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

【讨论】:

    【解决方案2】:
    $tags = get_tags();
    $html = '<ul>';
    foreach ( $tags as $tag ) {
    if($tag->slug != "migliori"){
    $tag_link = get_tag_link( $tag->term_id );
    $html .= "<li><a href='{$tag_link}' class='{$tag->slug}'>";
    $html .= "{$tag->name}</a></li>";
    
            }
    }
    $html .= '</ul>';
    echo $html;
    

    这行得通。

    【讨论】:

      【解决方案3】:

      我试过这个并为我工作。

      <?php
          $tags = get_tags();
          if ($tags) {
      ?><ul class="tags"><?php
          foreach ($tags as $tag) {
              echo '<li><a href="' . get_tag_link( $tag->term_id ) . '" 
                    title="' . sprintf( __( "View all posts in %s" ), $tag-
                    >name ) . '" ' . '>' . $tag->name.'</a></li>';
          }
          echo '<li><a href="#">View All</a><span class="arrow"></span>
      </li>'; ?></ul>
      <?php }?>   
      

      【讨论】:

        【解决方案4】:
        edit this code and work for me !!!
        <ul class="tags">
        
        
            <?php
        
            $tags = get_tags('post_tag'); //taxonomy=post_tag
            //echo "<pre>";
            //print_r($tags);
            //var_dump($tags);
            if ( $tags ) :
                foreach ( $tags as $tag ) : ?>
                    <li><a class="tag" href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
                <?php endforeach; ?>
            <?php endif; ?>
        </ul>
        

        【讨论】:

        • 您能对此代码添加一些解释吗?对我来说,它看起来像是其他答案的乱码
        • 但是这段代码显示了所有的标签。但我展示了特定的单个帖子标签列表。你能用这个代码吗?
        • ', '
        • ', '
        • ' ) ; ?>
        猜你喜欢
        相关资源
        最近更新 更多
        热门标签