建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

WordPress 获取某个类别及其子类别中的文章数量

GG网络技术分享 2025-03-18 16:04 0


首先定义实现函数,将以下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);?>

标签: WordPress 教程

提交需求或反馈

Demand feedback