希望这能帮助:
function wpse_display_post_counts_by_categories() {
$categories = get_categories();
$container = [];
foreach ($categories as $category) {
$args = [
\'post_type\' => \'post\', // Change to your post type.
\'post_status\' => \'publish\',
\'category\' => $category->term_id,
\'orderby\' => \'post_date\',
\'order\' => \'DESC\',
\'posts_per_page\' => -1
];
$posts = get_posts($args);
if ( count($posts) ) { // Indexing by post date of the latest post in the category.
$container[strtotime($posts[0]->post_date)] = [
\'id\' => $category->term_id,
\'name\' => $category->name,
\'counts\' => count($posts)
];
}
}
krsort($container); // Sort by key(post date), from high to low to get latest updated categories.
$values = array_slice($container, 0, 3, true); // Get only 3 latest updated categories.
foreach ($values as $key => $value) {
$output = sprintf( __(\'%s, there are %d articles posted in category <a href="%s">%s</a>\', \'text-domain\'), date_i18n(\'Y/m/d\', $key), $value[\'counts\'], esc_url( get_term_link($value[\'id\'], \'category\') ), $value[\'name\']);
echo \'<p>\' . $output . \'</p>\'; // Whatever markup you need.
}
}
IMHO:目前,我还没有找到一种更有效的方法。我不知道你怎么处理
$categories
当有太多的东西无法循环时。我不建议这样做。仅仅为了打印出一些状态就太多了。