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.
Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.
We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.
If you like our content, and would like to help, please consider making a contribution:
Ghacks is a technology news blog that was founded in 2005 by Martin Brinkmann. It has since then become one of the most popular tech news sites on the Internet with five authors and regular contributions from freelance writers.