get all tags of published posts Wordpress

2019-09-08 07:55发布

How do I fetch all tag names (with ID and count) from all published posts?

I know there is wp_tag_cloud but I only want an array of all tags.

标签: php wordpress
2条回答
啃猪蹄的小仙女
2楼-- · 2019-09-08 08:15

Check out get_terms() to retrieve the terms in a given taxonomy:

http://codex.wordpress.org/Function_Reference/get_terms

Examples:

$categories = get_terms( 'category',    'orderby=count&hide_empty=0' );

$tags       = get_terms( 'post_tag',    'orderby=count&hide_empty=0' );

$myterms    = get_terms( 'my_taxonomy', 'orderby=count&hide_empty=0' );
查看更多
The star\"
3楼-- · 2019-09-08 08:31

This works for me along with tags count.

<?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.' (' . $tag->count . ')</a></li>';
}
echo '<li><a href="#">View All</a><span class="arrow"></span>
</li>'; ?></ul>
<?php }?>   
查看更多
登录 后发表回答