【发布时间】:2016-03-24 19:04:06
【问题描述】:
在列出类别时,我想显示有多少帖子,包括子类别。我试过这个:
$cat_parent_ID = isset( $cat_id->category_parent ) ? $cat_id->category_parent : '';
if ($cat_parent_ID == 0) {
$tag = $cat_id;
} else {
$tag = $cat_parent_ID;
}
$q = new WP_Query( array(
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $tag,
'include_children' => true,
),
),
'fields' => 'ids',
) );
$allPosts = $q->post_count;
echo $allPosts;
?>
<?php _e( 'posts found', 'agrg' ); ?>
如果类别没有孩子,上述工作正常。但是,如果我单击具有子类别的类别,即使有帖子,我也会看到 0 posts found,但它们都在子类别中(因此父类别中有 0 个帖子,但子类别中有一些帖子)
我哪里出错了,我应该改变什么?
【问题讨论】: