Set the filter dynamically based on the page you are loading. If category archive pages have a 100 word excerpt, but posts have a 10 word excerpt, and everything else uses the default:
function my_custom_excerpt_length( $orig ) {
if( is_category() ) {
return 100;
} elseif( is_single() ) {
return 10;
}
return $orig;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length' );
You can get even more specific, giving specific categories a different length.
In response to Rarst, a new template function the_excerpt_length()
for outputting the excerpt with a specific number of words:
// Excerpt with a specific number of words, i.e. the_excerpt( 100 );
function the_excerpt_length( $words = null ) {
global $_the_excerpt_length_filter;
if( isset($words) ) {
$_the_excerpt_length_filter = $words;
}
add_filter( 'excerpt_length', '_the_excerpt_length_filter' );
the_excerpt();
remove_filter( 'excerpt_length', '_the_excerpt_length_filter' );
// reset the global
$_the_excerpt_length_filter = null;
}
function _the_excerpt_length_filter( $default ) {
global $_the_excerpt_length_filter;
if( isset($_the_excerpt_length_filter) ) {
return $_the_excerpt_length_filter;
}
return $default;
}
This would be used in your template file, i.e.:
<div class="entry">
<?php the_excerpt_length( 25 ); ?>
</div>