首先,定义实现函数,将以下php代码复制到当前主题的functions.php中:

function mobantu_get_cat_postcount($id) {  //id是分类ID     // 获取当前分类信息     $cat = get_category($id);       // 当前分类文章数     $count = (int) $cat->count;       // 获取当前分类所有子孙分类     $tax_terms = get_terms('category', array('child_of' => $id));       foreach ($tax_terms as $tax_term) {        // 子孙分类文章数累加        $count +=$tax_term->count;     }     return $count; }    //使用方法  <?php echo 'ID为123的分类及其子孙分类的文章数量为:' . mobantu_get_cat_postcount(123);?> 
www.npspro.cn软师兄
软师兄 » WordPress获取分类及其子分类文章数
50T免费网盘资源大集合【持续更中~~~~】:点击查看