There are times you need to have multiple excerpt lengths. You might have a page (usually the homepage) that in one section displays more text that it does in another section. As all things WordPress, there are a number of ways to do this. There might even be a number of plugins for this. You can achieve this easily using some code in your theme’s functions.php. To functions.php, add this:


/**
* Change the excerpt length. Returns a different excerpt length
* for use in any template file. For this example, we return a different
* excerpt length for two categories, 3 and 4
*/
function themename_excerpt_length ($length) {
if ( in_category( 3 )) { /*If the category has ID 3*/
return 10; //10 is how long the excerpt will be for this category
}
else if ( in_category( 4 ) ) { /*A different category ID*/
return 40;
}
else{
return 55; //The default excerpt length
}
}
add_filter('excerpt_length', 'themename_excerpt_length');

To use this function, while in the loop in your template file, call themename_excerpt_length(). I use this on my homepage to have different excerpt lengths in the different sections

Leave a Reply

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