为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
红糖糍粑 2023-08-11 17:45:05
我在带有类别行的父帖子类型存档页面上显示每个类别 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。我希望这是有道理的。感谢您的帮助。
查看完整描述

1 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

尝试使用三元运算符:

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



查看完整回答
反对 回复 2023-08-11
  • 1 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信