Custom excerpt length on home page

I thought that I got this working but unfortunately it is not. I need my static home page to have an excerpt length of 50 and every other pages to have one of 20. Here is my code so far:

function custom_excerpt_length( $length ) {
   if(is_front_page()) $content='excerpt';
   return 50;
}

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

I can tell it’s not working because on seamlessthemes.com the excerpt is 50, but on seamlessthemes.com/themes the excerpt is still 50. Please note this theme had a default one of 20 for some reason, so the themes page should still be 20.

Read More

Thanks in advance!

Related posts

Leave a Reply

1 comment

  1. 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;
    }