Post Pic

WordPress tip : change excerpt length depending of the category

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.

4 Responses

Oct 05 2011 12:42

Doesn’t work :( Guessing a wordpress update has made this obsolete

Feb 12 2012 23:06

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’);
}

Apr 07 2012 11:46

This worked perfectly for me, in the functions file.

Thank you – life saver!

Jun 25 2012 20:56

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

All the best
Dave

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox