亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帖子數量 - 顯示數量(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 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號