简体   繁体   English

计算自定义帖子类型分类法 (WordPress) 中的帖子数量

[英]Count number of posts in a custom post type taxonomy (WordPress)

I am trying to return a list of all categories in a custom taxonomy (shoes), along with the number of posts each category has in it (the post type is 'items' if it helps to know).我正在尝试返回自定义分类法(鞋子)中所有类别的列表,以及每个类别中包含的帖子数量(如果有助于了解,帖子类型是“项目”)。 But my code doesn't seem to show the number of posts:但我的代码似乎没有显示帖子的数量:

<ul>
<?php
$shoes = array(
        'taxonomy'      => 'shoes',
        'hide_empty'    => 0,
        'orderby'       => 'name',
        'order'         => 'ASC',
        'hierarchical'  => 0
    );
    $count_query = new WP_Query( $shoes );
    $cats = get_categories($shoes);
    foreach($cats as $cat) {
?>
    <li>
        <a href="<?php echo get_category_link( $cat->term_id ) ?>"><?php echo $cat->name; ?><span><?php echo $count_query->found_posts; ?></span></a>
    </li>
<?php
}
?>
</ul>

The category name and links work, but the number of posts (inside the <span> ) always returns 1 regardless of how many posts there actually are.类别名称和链接有效,但无论实际有多少帖子,帖子数(在<span>内)总是返回 1。 What am I doing wrong?我究竟做错了什么?

<ul>
    <?php
    $shoes = array(
            'taxonomy'      => 'shoes',
            'hide_empty'    => 0,
            'show_count' => 1, // if add this value you can use echo $cat->count; without any queries
            'orderby'       => 'name',
            'order'         => 'ASC',
            'hierarchical'  => 0
        );
        $count_query = new WP_Query( $shoes );
        $cats = get_categories($shoes);
        foreach($cats as $cat) {
    ?>
        <li>
            <a href="<?php echo get_category_link( $cat->term_id ) ?>"><?php echo $cat->name; ?><span><?php echo $cat->count; ?></span></a>
        </li>
    <?php
    }
    ?>
    </ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM