PHP文字基于类别中的帖子数

问题描述 投票:0回答:1

[我正在尝试制作一个WordPress短代码,该短代码应显示包含特定类别中帖子数量的文本,如果该类别为空,则应返回一个文本,指出该类别为空。

到目前为止,我编写的简码还算有效。唯一的问题是,即使该特定类别中的帖子很多,它也始终返回0个帖子。

我尝试了不同的功能,例如is_category()和get_category(),但均不起作用。类别与自定义帖子类型相关是否有区别?帖子类型的子弹是projekt btw。

function imbro_aaben_projekt_shortcode() {
    $category = get_category('aaben-projekt');
    $theCount = $category->count;

    if ( $theCount > 0 ){

        return 'Total: ' . $theCount . ' posts in this category';

    } else {

        return 'There are no posts in this category';

    }
}

add_shortcode( 'imbro_empty', 'imbro_aaben_projekt_shortcode' );
php wordpress count categories
1个回答
0
投票
获得所需结果的另一种方法是使用如下的WP_QUERY

$args = array( 'cat' => 4, // category id 'post_type' => 'post' ); $the_query = new WP_Query( $args ); echo $the_query->found_posts;

© www.soinside.com 2019 - 2024. All rights reserved.