How to show 5 top categories in Wordpress - gHacks Tech News

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.

wordpress show categories

So, the following arguments are used:

  • number=5
  • orderby=count
  • order=DESC
  • title_li=

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.

Summary
How to show 5 top categories in WordPress
Article Name
How to show 5 top categories in WordPress
Description
How to display the top 5 categories of a WordPress blog on a site running the blogging software using a single line of code.
Author
Publisher
Ghacks Technology News
Logo

We need your help

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:

Comments

  1. Lance said on April 9, 2009 at 4:30 pm
    Reply

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

    Thanks.

  2. Daniel Pataki said on April 9, 2009 at 5:09 pm
    Reply

    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 said on April 11, 2009 at 6:44 pm
    Reply

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

  4. Addicott Web said on April 16, 2009 at 6:32 pm
    Reply

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

  5. Glannagroxons said on April 17, 2009 at 1:39 am
    Reply

    nice, really nice!

  6. romany said on April 20, 2009 at 1:34 am
    Reply

    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

  7. nikitha said on November 13, 2010 at 1:04 pm
    Reply

    may i know where to add the code ??? please help me …

    thanks in advance ….

  8. Arc said on March 23, 2015 at 12:26 pm
    Reply

    What if I want to display horizontally like –> Fashion (3) Music (2)

  9. Jack said on April 29, 2016 at 5:15 pm
    Reply

    Nice short and simple code. Although i wanted to know if it is possible to exclude Uncategorized from the list without using css.

  10. ramjam said on November 16, 2016 at 8:24 pm
    Reply

    this light typo make my eyes bleed! couldn’t really read the article.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.