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.

8 Responses

Dec 02 2009 09:50

What a great idea! I don’t have any ideas right now of how to use this, but it’s still pretty cool.

Dec 02 2009 11:13

Yah for my tip being published. thanks!

@Eric B. I find it very useful because when i have categories for news, events, blog etc they always need different lengths because they have different designs. This tip is more for when using WordPress as a cms rather than blogging platform.

Dec 05 2009 14:36

Cool Tip & Agreed with Dave.. really useful when using WordPress as CMS.. a really great one for article and news sites ! ;)

Dec 10 2009 08:01

Thx Dave, its help me when i need to display excerpt in fix box or something

Dec 24 2009 11:02

Very useful, thanks… Great blog :) I love it!

May 11 2010 17:10

That is what I was looking for:)
And how to change the excerpt length for tags?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required