好吧,似乎这不是现成的支持。至少我没找到arguments you can use for WP_Term_Query
(这是后面使用的功能get_categories()
).
所以我有以下想法
检索所有类别从每个类别获取最新帖子保存类别id(&L);在数组中发布日期
按发布日期排序返回5个最近的类别IDfunction get_most_recent_categories($limit = 5) {
// retrieve all categories
$categories = get_categories();
$recent_posts = array();
foreach ($categories as $key=>$category) {
// get latest post from $category
$args = array(
\'numberposts\' => 1,
\'category\' => $category->term_id,
);
$post = get_posts($args)[0];
// save category id & post date in an array
$recent_posts[ $category->term_id ] = strtotime($post->post_date);
}
// order by post date, preserve category_id
arsort($recent_posts);
// get $limit most recent category ids
$recent_categories = array_slice(array_keys($recent_posts), 0, $limit);
return $recent_categories;
}