如何按其最近帖子的发布日期获得5个类别的排序?
我基本上需要在最近的帖子中使用的5个最新类别。
如何按其最近帖子的发布日期获得5个类别的排序?
我基本上需要在最近的帖子中使用的5个最新类别。
好吧,似乎这不是现成的支持。至少我没找到arguments you can use for WP_Term_Query
(这是后面使用的功能get_categories()
).
所以我有以下想法
检索所有类别从每个类别获取最新帖子保存类别id(&L);在数组中发布日期
function 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;
}
我目前有6个职位属于“服务”类别。每个服务都有\\u内容和1-2个文档(小册子和/或T&C以及外部表单)现在,假设这6篇文章的ID为1,2,3,4,5,6,但我希望它是2,5,3,1,4,6。这可能吗?如果是,我将如何实现它?