首先,定义实现函数,将以下php代码复制到当前主题的functions.php中:
function wpmao_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的分类及其子孙分类的文章数量为:' . wpmao_get_cat_postcount(123);?>