我在帶有類別行的父帖子類型存檔頁面上顯示每個類別 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>';
- 1 回答
- 0 關注
- 147 瀏覽
添加回答
舉報
0/150
提交
取消