Modified version of get_the_category to limit the number of categories displayed.


Some times as a theme developers we may have to display only specific number of categories a post is filed in as a large number of categories may break the layout in some cases and sometimes we may have to display only one category.

For example in SWIFT magazine layout, I display one category overlayed on the thumbnail.

Unfortunately WordPress functions get_the_category(), the_category() do not take any arguments and display all the categories the post is filed in. I wrote this small function during the development of swift to echo a specific number of categories a post is filed in. I hope this will help the fellow theme developers

Usage is similar to the_category() function, except we take arguments here.

If you have any questions feel free to drop them in comments

9 thoughts on “Modified version of get_the_category to limit the number of categories displayed.

  1. So after beating my head against the wall for a few hours trying to solve this very problem, I just stumbled across this script. And it is EXACTLY what I need!

    Dumped it into my theme-functions.php file, and as soon as I refresh, I get a 500 error for my entire site.
    I’m obviously doing something wrong.

    I then tried dumping it into its own php function file (saving it in the $functions_path directory), but no dice with that, either.

    Any ideas what might be happening here? Would love to standardize my design so it’s not showing me extra categories where I don’t need them.

    • (And if it doesn’t show, I’m obviously a PHP newb, still working out the kinks). Thanks again for this snippet! Hoping I can get it to work!

  2. Great post! Very informative, I was actually having alot of trouble with the WordPress php functions and even tried hiding the categories with CSS in my website. This mod rocks!

  3. hey, great function!! I was wondering if there is anyway to exclude categories from being listed?
    Again thanks a lot for the function! it really is great!

  4. Thank you! This came just in time when the higher-ups were asking me if this was possible since some of our posts had a lot of categories attached and thus, messing up the layout. I got it done in 5 minutes after reading this tutorial. :) Good job.

  5. [...] to swiftthemes.com, this function came in handy when I had to modify the way categories were being called in the [...]

  6. I really love this theme, but recently, the swift table seems does’t work well, could you help me ?

  7. Hi there, I love your work, I have had the 4.0 version working nicely on my site, and my seo is working really considering I have only had the site running for 1 month. I will move to 5.0 but found it a little buggy for now so I went back to 4. But great job. I like the java slider :)
    If you want to see my site it is http://www.healthinformatics.info

Leave a Reply

Your email address will not be published. Required fields are marked *