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

[php]
/*
A custom function to echo specified number of categories a
post is filed in.
(Takes number of categories to be displayed as argument)
Written by Satish Gandham
Author URL: http://swiftthemes.com
Contact: http://swiftthemes.com/contact-me/
*/
function swift_list_cats($num){
$temp=get_the_category();
$count=count($temp);// Getting the total number of categories the post is filed in.
for($i=0;$i<$num&&$i<$count;$i++){
//Formatting our output.
$cat_string.='<a href="’.get_category_link( $temp[$i]->cat_ID ).’">’.$temp[$i]->cat_name.'</a>’;
if($i!=$num-1&&$i+1<$count)
//Adding a ‘,’ if it’s not the last category.
//You can add your own separator here.
$cat_string.=’, ‘;
}
echo $cat_string;
}
[/php]

Usage is similar to the_category() function, except we take arguments here.
[php]
Filed under <?php swift_list_cats(3); ?>
[/php]

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

10 Replies to “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.

    1. (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!

      1. It didn’t work for me either in the first place, but then I changed the ‘ (air quotes). And it worked. (Don’t know if that was your problem… 5 years ago)

        Super happy, thanks! Been looking for this for an hour.

  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. Pingback: Wordpress: Limiting number of categories displayed on post | Stephanie Chung
  6. 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 *