[英]Number of Posts - Display number (x of x), if number is less than 4 ( 4 is the 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>';
问题是,我的一些查询少于 4 个posts_per_page。 所以我想最多计算 4 个,所以如果值小于 4 个帖子,它只会计算那个数字。 例如,如果有 2 个帖子,它会说 2 of 2。但如果有 30 个帖子,它会说 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>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.