我有一个函数,可以从每个类别中提取最新的帖子,并按最新的帖子排列类别。我从中得到的是一个带有类别ID的数组,我想用它来重新排列get\\u categories数组。
/* Get all categories */
$categories = get_categories();
/* Create empty array */
$categories_order = [];
/* For each category */
foreach($categories as $category) {
/* Modify WP query */
$args = array(\'posts_per_page\' => 1, /* Max 1 post */
\'category__in\' => array($category->term_id), /* In this specific category */
\'ignore_sticky_posts\' => true ); /* No sticky posts */
/* Get all posts from categories with modifier */
$posts = get_posts($args);
/* If there are posts */
if ($posts) {
/* For each post */
foreach($posts as $post) {
/* Add to array key => value (category id => time published) */
$categories_order[$category->term_id] = get_post_time(\'YmdHis\');
}
}
}
arsort($categories_order); /* Order new array by value */
$categories_order = array_keys($categories_order); /* Remove array values */
print_r($categories_order);
My函数返回:Array ( [0] => 2 [1] => 5 [2] => 4 )
Array
(
[1] => stdClass Object
(
[term_id] => 2
[name] => Спорт
[slug] => sport
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 6
[cat_ID] => 2
[category_count] => 6
[category_description] =>
[cat_name] => Спорт
[category_nicename] => sport
[category_parent] => 0
)
[2] => stdClass Object
(
[term_id] => 4
[name] => Дом и градина
[slug] => home-and-garden
[term_group] => 0
[term_taxonomy_id] => 4
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 4
[category_count] => 1
[category_description] =>
[cat_name] => Дом и градина
[category_nicename] => home-and-garden
[category_parent] => 0
)
[3] => stdClass Object
(
[term_id] => 5
[name] => Транспорт
[slug] => transport
[term_group] => 0
[term_taxonomy_id] => 5
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[cat_ID] => 5
[category_count] => 1
[category_description] =>
[cat_name] => Транспорт
[category_nicename] => transport
[category_parent] => 0
)
)
现在不知何故,get_categories term_id应该与我的数组进行比较,并相应地重新排列对象。