0
A
回答
3
試試:
function get_sorted_categories($order_by = 'id', $args = array()){
global $wpdb;
$category = get_categories($args);
$order = [
'id' => 'post.ID',
'date' => 'post.post_date',
'modified' => 'post.post_modified',
];
$order_by = $order[ $order_by ];
$q = $wpdb->get_results("SELECT tax.term_id FROM `{$wpdb->prefix}term_taxonomy` tax
INNER JOIN `{$wpdb->prefix}term_relationships` rel ON rel.term_taxonomy_id = tax.term_id
INNER JOIN `{$wpdb->prefix}posts` post ON rel.object_id = post.ID WHERE tax.taxonomy = 'category' AND post.post_type = 'post' AND post.post_status = 'publish' ORDER BY {$order_by} DESC");
$sort = array_flip(array_unique(wp_list_pluck($q, 'term_id')));
usort($category, function($a, $b) use ($sort, $category) {
if(isset($sort[ $a->term_id ], $sort[ $b->term_id ]) && $sort[ $a->term_id ] != $sort[ $b->term_id ])
$res = ($sort[ $a->term_id ] > $sort[ $b->term_id ]) ? 1 : -1;
else if(!isset($sort[ $a->term_id ]) && isset($sort[ $b->term_id ]))
$res = 1;
else if(isset($sort[ $a->term_id ]) && !isset($sort[ $b->term_id ]))
$res = -1;
else
$res = 0;
return $res;
});
return $category;
}
print_r(get_sorted_categories());
print_r(get_sorted_categories('date'));
print_r(get_sorted_categories('modified'));
獲取按類別順序(後ID |發佈日期|後修改的日期)。沒有任何循環和快速!
1
我認爲最簡單的方法是遍歷post_date
訂購的所有帖子,然後將類別保存到數組中。然後,您可以遍歷類別並顯示它們。他們將是有序的。
事情是這樣的:
<?php
// Initiate array
$cats = array();
// Query arguments
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' = 'post_date'.
'order' => 'DESC'
);
// The query
$query = new WP_Query($args);
// The loop
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
// Get the term object
$term = get_the_category();
// Make sure the term doesn't already exist in the array
if(!array_key_exists($term[0]->ID, $cats)) {
// Add the terms to the array
$cats[$term[0]->ID] = $term;
}
}
}
foreach($cats as $cid => $cat) {
// Loop through the categories here
}
?>
當然,作爲在評論中提及上面的,你也可以做到這一點的其他方式和遍歷類第一則數組排序。這樣的事情可以做:
<?php
// Initiate array
$cats = get_categories();
$recent_cats = array();
foreach($cats as $k => $cat) {
// Query arguments
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' = 'post_date'.
'order' => 'DESC',
'cat' => $cat->term_id
);
// The query
$query = new WP_Query($args);
// The loop
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
$date_str = strtotime(the_date());
if(!array_key_exists($date_str, $recent_cats)) {
$recent_cats[$date_str] = $cat->name;
}
}
}
}
krsort($recent_cats);
// Loop through $recent_cats here
?>
+0
感謝您嘗試幫助!我的網站有成千上萬的帖子和幾十個類別,肯定會影響功能,請注意,你有一個拼寫錯誤'orderby'='post_date'.'應該是''orderby'=>'post_date','和代碼返回'array_key_exists():第一個參數應該是一個字符串或一個整數'。 – tinyCoder
相關問題
- 1. 在發佈後發佈/訂閱通知
- 2. 訂單後通過meta值
- 3. Woocomerce更改訂單$ product-> get_categories
- 4. 通過自定義元分頁的Wordpress訂單發佈
- 5. 通過訂單
- 6. WordPress的發佈訂單
- 7. 通過發佈日期和時間戳違規MYSQL訂單
- 8. WordPress的 - 如何訂購get_categories結果
- 9. 通過全球/後端訂單(orderby)訂購WordPress的屬性
- 10. 同步發佈訂單
- 11. 訂單發佈集數(Wordpress)
- 12. Android的Firebase訂單通過
- 13. 如何通過表單發送簡單的訂購清單?
- 14. 編輯HTML然後通過PHP發佈
- 15. php表單發佈數組訂單
- 16. AJAX生成的訂單列表li id不通過數據發佈
- 17. 通過使用foreach的動態表單通過Ajax發佈
- 18. 將表單域傳遞給ajax,然後通過php發佈它
- 19. AngularJS自定義訂單訂購通過
- 20. 如何通過特定訂單訂購?
- 21. 通過HTML表單發佈XML
- 22. 如何通過C發佈表單#
- 23. 通過點擊表單發佈div
- 24. 通過javascript發佈表單嗎?
- 25. 通過javascript使用Sitecore發佈表單
- 26. 缺少訂閱的消息通過服務質量發佈= 2
- 27. SQL訂單和組通過
- 28. Bigcommerce通過訂單分頁
- 29. SQl通過訂單查詢
- 30. 通過查詢MYSQL訂單
我會首先檢索所有類別,然後通過類別循環獲取最後一篇文章,並根據發佈日期將其存儲在一個數組中,然後對結果數組進行排序 – fabio