简体   繁体   English

帖子数量 - 显示数量(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)

I am showing 4 custom post type posts per category on a parent post type archive page with category rows.我在带有类别行的父帖子类型存档页面上显示每个类别的 4 个自定义帖子类型帖子。 I want to show the count of the category posts ($list->found_posts) , but I am limiting the displayed posts to a random 4 posts.我想显示类别帖子($list->found_posts)的计数,但我将显示的帖子限制为随机 4 个帖子。

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

I have successfully displayed the total number of posts for each category (business_listing is the post type)我已成功显示每个类别的帖子总数(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>';

在此处输入图像描述

The issue is, that some of my queries have less than the 4 posts_per_page.问题是,我的一些查询少于 4 个posts_per_page。 So I wanted to count UP TO 4, so if the value is less than 4 posts it will only count that number.所以我想最多计算 4 个,所以如果值小于 4 个帖子,它只会计算那个数字。 For example if there are 2 posts, it will say 2 of 2. But if there are 30 posts, it would say 4 of 30.例如,如果有 2 个帖子,它会说 2 of 2。但如果有 30 个帖子,它会说 4 of 30。

I hope this makes sense.我希望这是有道理的。 Thanks for your help.谢谢你的帮助。

Try using the ternary operator:尝试使用三元运算符:

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.

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