Custom excerpt length on home page

How filters work

Filters are simply a point where you can take some value, that is set (for something) in core (for e.g. the the_excerpt() function), in open to modifications.

So…

$some_var = apply_filters( 'change_this_var', 50 );

…just says:

»Some var« is set to 50, but when you add a filter callback to change_this_var, then you can get, modify and return the value.

Excercise

Here’s an example, based on your question, that gets the variable as parameter in the function definition (the definition of the “filter callback”-function). It then modifies (in case – we use conditional tags here) it and returns it.

add_filter( 'excerpt_length', 'wpse61271_custom_excerpt_length', PHP_INT_MAX -1 );
function wpse61271_custom_excerpt_length( $length ) 
{
    return ( 
       is_front_page()
       XOR is_home()
    )
       ? 50
       : $length;
}

Leave a Comment