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

我在带有类别行的父帖子类型存档页面上显示每个类别 4 个自定义帖子类型帖子。我想显示类别帖子的数量($list->found_posts),但我将显示的帖子限制为随机 4 个帖子。


<?php $list = new WP_Query (array(

 'posts_per_page' => 4,

 'post_type' => 'business_listing',

 'category__in' => 5,

 'orderby' => 'rand'

 )) ;

我已成功显示每个类别的帖子总数(business_listing 是帖子类型)


$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>';

https://img.mukewang.com/64d6033e00014a0806420058.jpg

问题是,我的一些查询少于 4 posts_per_page。所以我想计算最多 4 个,所以如果该值小于 4 个帖子,它只会计算该数字。例如,如果有 2 个帖子,则会显示 2 of 2。但如果有 30 个帖子,则会显示 4 of 30。


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


红糖糍粑
浏览 75回答 1
1回答

白衣染霜花

尝试使用三元运算符:echo&nbsp;'<h3&nbsp;class="directory&nbsp;text-left">'.&nbsp;get_cat_name($category_id&nbsp;=&nbsp;'5')&nbsp;.'&nbsp;('.&nbsp;($list->found_posts&nbsp;<&nbsp;4&nbsp;?&nbsp;$list->found_posts&nbsp;:&nbsp;4)&nbsp;&nbsp;.'&nbsp;of&nbsp;'.&nbsp;$list->found_posts&nbsp;.')&nbsp;<a&nbsp;href="'.&nbsp;get_category_link($listCat)&nbsp;.'">View&nbsp;All</a></h3>';
打开App,查看更多内容
随时随地看视频慕课网APP