wordpress统计某个标签下的文章总数 wordpress


function.php

1、根据Tag的ID调用

//根据标签ID获取文章数
function get_tag_post_count_by_id( $tag_id ) {
       $tag = get_term_by( 'id', $tag_id, 'post_tag' );
      _make_cat_compat( $tag );
       return $tag->count;
}

2、根据Tag的名称调用

//根据标签别名获取文章数
function get_tag_post_count_by_slug( $tag_slug ) {
       $tag = get_term_by( 'slug', $tag_slug, 'post_tag' );
       _make_cat_compat( $tag );
      return $tag->count;
}

3、前台调用

<?php  echo  get_tag_post_count_by_id( $tag_id )    ?>
<?php  echo  get_tag_post_count_by_id( $tag_slug )    ?>

上面我们通过两个小函数就可以实现通过标签ID或者标签的别名来取得所指定标签下的文章数量了。


签名:这个人很懒,什么也没有留下!
最新回复 (0)
返回