DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Do you ever wished to be able to modify the excerpt length based on which category you are on, without modifying your theme files? If yes, I’m pretty sure you’ll be happy with that recipe.

No need to modify your theme files. Simply paste the code into your functions.php file. Don’t forget to change the category ID on line 3!

add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($length) {
    if(in_category(14)) {
        return 13;
    } else {
        return 60;

Thanks to Dave Redfern for contributing to WpRecipes!

Also, that might interest you , PremiumMod are launching a WordPress Theme design contest. Check it out here.



  1. Doesn’t work 🙁 Guessing a wordpress update has made this obsolete

  2. It worked perfectly in my theme, but in the matter of use it in a specific part of my template, instead of write the code in the function.php file I put it into the header.php above the DOCTYPE tag and using the conditional
    if (is_home()) {
    function my_excerpt_length($length) {
    if (in_category(1)) {
    return 9;
    add_filter(‘excerpt_length’, ‘my_excerpt_length’);

  3. This worked perfectly for me, in the functions file.

    Thank you – life saver!

  4. I find myself coming back here more and more. You ROCK and so does this little jewel!!

    All the best

Leave a Comment

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