帖子数量 - 显示数量(x of x),如果数量小于 4(4 是 post_per_page)

Number of Posts - Display number (x of x), if number is less than 4 ( 4 is the post_per_page)

我在包含类别行的父 post 类型存档页面上为每个类别显示 4 个自定义 post 类型 post。我想显示类别 posts ($list->found_posts) 的计数,但我将显示的 posts 限制为随机 4 posts.

<?php $list = new WP_Query (array(
 'posts_per_page' => 4,
 'post_type' => 'business_listing',
 'category__in' => 5,
 'orderby' => 'rand'
 )) ;

我已经成功显示了每个类别的post总数(business_listing是post类型)

$listCat = get_category('5');
$catName = get_field('display_name', $listCat);
$numberPosts = wp_count_posts($list);
echo '<h3 class="directory text-left">'. get_cat_name($category_id = '5') .' ('. $list->found_posts .' of '. $list->found_posts .') <a href="'. get_category_link($listCat) .'">View All</a></h3>';

问题是,我的一些查询少于 4 posts_per_page。所以我想最多数到 4,所以如果该值小于 4 posts,它只会计算那个数字。例如,如果有 2 posts,它会说 2 of 2。但是如果有 30 posts,它会说 4 of 30.

我希望这是有道理的。感谢您的帮助。

尝试使用三元运算符:

echo '<h3 class="directory text-left">'. get_cat_name($category_id = '5') .' ('. ($list->found_posts < 4 ? $list->found_posts : 4)  .' of '. $list->found_posts .') <a href="'. get_category_link($listCat) .'">View All</a></h3>';