ghacks Technology News

How to show 5 top categories in Wordpress


wordpressWordpress 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. 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=’) ?>

ScriptIf you’d like to read some similar articles, take a look at Scriptastique, a blog all about web development and coding, with great tips on CSS, HTML, PHP, MySQL and Javascript and tutorials and screencasts coming soon! You can follow us on our RSS feed, or Twitter where we’re posting 3-4 short tips daily now!




Tags: , , , ,
Categories: Web Development


Read Related Posts


9 Responses to “How to show 5 top categories in Wordpress”

  1. Lance says:

    Great. Is there a way to show most viewed categories?

    Thanks.

  2. Hi Lance!

    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,

    Daniel

  3. Tobey says:

    Nice. Some time ago I tried to deal with similar prob in WP. Thanks for sharing this tip.

  4. Addicott Web says:

    This is exactly what I was looking for – thanks for the tip!

  5. nice, really nice!

  6. romany says:

    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

Trackbacks/Pingbacks

  1. [...] Post gHacks – How to show 5 top categories in Wordpress [...]

  2. [...] View original post here: How to show 5 top categories in Wordpress [...]

  3. [...] find anything. A simple Google search solved my problem, and I found the belong article: How to show 5 top categories in Wordpress. It was such a simple solution, I was so silly not to think of [...]

Leave a Reply   Follow Ghacks   Subscribe To Comment Rss

© 2005-2009 Ghacks.net. All Rights Reserved. Privacy Policy - About Us