这是我碰上几次,我改变the code from PatchRanger on bountify,使其与分类工作(所以请注意这个解决方案是基于他的作品):
function wp_get_term_postcount($term) {
$count = (int) $term->count;
$tax_terms = get_terms($term->taxonomy, array('child_of' => $term->term_id));
foreach ($tax_terms as $tax_term) {
$count += wp_get_term_postcount_recursive($tax_term->term_id);
}
return $count;
}
function wp_get_term_postcount_recursive($term_id, $excludes = array()) {
$count = 0;
foreach ($tax_terms as $tax_term) {
$tax_term_terms = get_terms($tax_term->name, array(
'child_of' => $tax_term->term_id,
'exclude' => $excludes,
));
$count += $tax_term->count;
$excludes[] = $tax_term->term_id;
$count += wp_get_term_postcount_recursive($tax_term->term_id, $excludes);
}
return $count;
}
的递归功能是为了防止孩子的重复计数。您可以在functions.php中添加这两个函数。
然后更新您的代码使用它:
$categories = get_terms($taxonomy, $args);
foreach($categories as $categ) {
print $categ->name.'/'wp_get_term_postcount($categ);
}
你可能会有更多的运气在[Wordpress Exchange](http://wordpress.stackexchange.com/)上发布此信息。 – leigero