How to show 5 top categories in WordPress
WordPress is a great piece of software, and allows more than you'd think to be done with simple coding. I bet that a lot of people out there would look for a plugin to do this, but your top five categories can very easily be shown using a WordPress template tag, just a half a line of code.
The tag we need is called "wp_list_categories()" and by default it lists all your categories in alphabetical order. All we need to do is add some arguments to it to modify the default values and we're done! We need to limit the number of items shown to five, we also want to show the post counts in the categories and we want to order the list by the post count, in descending order.
The "number" argument actually has no default value. The reason for this is that it limits the SQL query directly, so there is no need for it by default. We can add it though to impose a limit, so our first argument will be "number=5". We also want to show the post counts, so we use "show_count=1" to enable this (show_count=0 is the default).
We also want to order the list by the post count, so we use "orderby=count" to achieve this. By default lists are sorted ascending, meaning that the lowest post count would show up first. To make the list descending, we can use "order=DESC".
I also use one last argument in there, which is "title_li=". This sets the title of the list to be nothing, by default the list is presented with a title.
So, the following arguments are used:
I like to hard code my titles for a few reasons, but feel free to change this as you like. So here is the complete code I used, one simple line to do a seemingly hard task, enjoy
<?php wp_list_categories('number=5&show_count=1&orderby=count&order=DESC&title_li=') ?>
All you have to do is embed the code into source, for instance in the sidebar, or another location of the WordPress theme that you are using.
You can modify it naturally, for instance to show more or less categories, or drop the number of posts per category count.
Great. Is there a way to show most viewed categories?
No, not this easily, WordPress doesn’t have a tracking system installed by default to do this. I think Popularity Contest by Alex King can display most viewed categories, but I’m not sure. If you are handy at coding this is not all that difficult to code in though.
If you need some help let me know,
Nice. Some time ago I tried to deal with similar prob in WP. Thanks for sharing this tip.
This is exactly what I was looking for – thanks for the tip!
nice, really nice!
how i get pages into pages in word press and how to make nambers for the psotes in the home page i wich u can help me thanx
may i know where to add the code ??? please help me …
thanks in advance ….
What if I want to display horizontally like –> Fashion (3) Music (2)
Nice short and simple code. Although i wanted to know if it is possible to exclude Uncategorized from the list without using css.
this light typo make my eyes bleed! couldn’t really read the article.