[英]How to sort categories by number of posts in wordpress?
I'm using this syntax for ordering categories by the number of posts in each categories. 我正在使用此语法按每个类别中帖子的数量对类别进行排序。
<?php wp_list_categories('title_li=&show_count=1&child_of=3&number=5&orderby=count'); ?>
Still it's not working.. It shows the categories ordered alphabetically. 仍然不起作用。.它按字母顺序显示类别。 Please help me!
请帮我!
Please see the sidebar 请参阅侧栏
Here is some code I have modified from another answer 这是我从另一个答案修改的一些代码
<?php
echo '<ul>';
foreach (get_categories('orderby=count&order=ASC') as $category )
{
if( $category->category_parent == '0')
{
$url = '';
$url = site_url() . '/' . $category->taxonomy . '/' . $category->slug ;
echo '<li class="cat-item cat-item-' . $category->term_id . '"><a href="' . $url . '">' . $category->name . ' (' . $category->count . ')</a></li>';
}
}
echo '</ul>';
?>
Here is the question that led to the answer. 这是导致答案的问题。
http://wordpress.org/support/topic/cant-get-wp_list_categories-to-list-by-count-help http://wordpress.org/support/topic/cant-get-wp_list_categories-to-list-by-count-help
Removing the $category->category_parent == '0'
if check, will mean that all categories will be selected and not just parents. 如果取消选中
$category->category_parent == '0'
,则意味着将选择所有类别,而不仅仅是父类别。
Hope this helps. 希望这可以帮助。
Update 更新
Change the line ('orderby=count&order=ASC')
to ('orderby=count&order=DESC')
for descending ordering. 将行
('orderby=count&order=ASC')
更改为('orderby=count&order=DESC')
以降序排列。 Also you will have to add your further arguments etc, ie &child_of=3&number=5
. 另外,您还必须添加其他参数,例如
&child_of=3&number=5
。
<?php
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date', <----------------
'order' => 'DESC', <----------------
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$posts_array = get_posts( $args );
?>
You can sort them how you want like menu_order,post_date
etc. 您可以根据需要对其进行排序,例如
menu_order,post_date
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.