有没有办法根据包含的帖子数量对类别/分类列表进行排序?
问候, Desizner
您可以使用 get_categories() 函数并在 'taxonomy' 中传递以下值之一:'category'(仅获取类别)或 'post_tag'(仅获取标签),甚至删除此键,它会同时获取两者。下面我们按每个类别 DESC 的帖子排序。
<?php
$categories = get_categories([
'taxonomy' => 'category',
'orderby' => 'count',
'order' => 'DESC'
]);
foreach ($categories as $category) {
// Do something
}
?>
目前(2017 年 9 月)我也使用wp_list_categories(作为 Ryan B)但使用以下代码:
<?php wp_list_categories( array(
'orderby' => 'count',
'order' => 'DESC'
) ); ?>
<?php
foreach (get_categories('orderby=count&order=DESC') as $category )
{
/*Some stuff here*/
}
?>
有关更多详细信息,请查看:https ://developer.wordpress.org/reference/functions/get_categories/