Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何显示wordpress类别(包括子类别)中的帖子数量?_Php_Wordpress - Fatal编程技术网

Php 如何显示wordpress类别(包括子类别)中的帖子数量?

Php 如何显示wordpress类别(包括子类别)中的帖子数量?,php,wordpress,Php,Wordpress,在列出类别时,我想显示有多少帖子,包括子类别。我试过这个: $cat_parent_ID = isset( $cat_id->category_parent ) ? $cat_id->category_parent : ''; if ($cat_parent_ID == 0) { $tag = $cat_id; } else { $tag = $cat_parent

在列出类别时,我想显示有多少帖子,包括子类别。我试过这个:

$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' ); ?>
$cat\u parent\u ID=isset($cat\u ID->category\u parent)$类别id->类别父项:“”;
如果($cat\u parent\u ID==0){
$tag=$cat_id;
}否则{
$tag=$cat\u parent\u ID;
}
$q=新的WP\U查询(数组(
“没有”=>正确,
“tax_query”=>数组(
排列(
“分类法”=>“类别”,
“字段”=>“id”,
“术语”=>$tag,
“包含子项”=>正确,
),
),
“字段”=>“ID”,
) );
$allPosts=$q->post\U计数;
echo$allPosts;
?>
如果类别没有child,则上面的操作很好。但如果我点击有子类别的类别,我会看到
0篇找到的帖子
,即使有帖子,但它们都在子类别中(所以父类别中有0篇帖子,但子类别中有一些帖子)


我哪里出错了?我应该更改什么?

尝试使用此功能:

function wp_get_cat_postcount($id) {
    $cat = get_category($id);
    $count = (int) $cat->count;
    $taxonomy = 'category';
    $args = array(
        'child_of' => $id,
    );
    $tax_terms = get_terms($taxonomy,$args);
    foreach ($tax_terms as $tax_term) {
        $count +=$tax_term->count;
    }

    return $count;
}

此函数将通过传递类别id返回指定类别及其子类别(如果有)的总帖子数量。我希望它对您有用,谢谢。

尝试使用此函数:

function wp_get_cat_postcount($id) {
    $cat = get_category($id);
    $count = (int) $cat->count;
    $taxonomy = 'category';
    $args = array(
        'child_of' => $id,
    );
    $tax_terms = get_terms($taxonomy,$args);
    foreach ($tax_terms as $tax_term) {
        $count +=$tax_term->count;
    }

    return $count;
}
此函数将通过传递类别id返回指定类别及其子类别(如果有)的总帖子数。我希望它对您有用,谢谢

您可以通过

<p><?php echo wt_get_category_count();?></p>

您可以通过

<p><?php echo wt_get_category_count();?></p>


如果一篇文章被分配到多个子类别,它会被多次计数,因此,如果一篇文章被分配到多个类别,或者它的父级和子级都被选中,那么使用此函数会得到错误的结果错误的结果。如果一篇文章被分配到多个子类别,它会被多次计数,因此,如果一篇文章属于多个类别,或者它的父项和子项都被选中,那么使用此函数会得到错误的结果错误的结果。