3

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.

Thanks in advance!

  • Are you the author of those child themes? – kaiser Aug 9 '12 at 1:16
  • Not sure what you mean. I have made thematic child themes before though if thats what you meant. – Jagst3r15 Aug 9 '12 at 1:21
7

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;
}
| improve this answer | |
  • thank you - worked perfectly. I need to learn this stuff too, so thanks for explaining. Not sure if I am allowed to vote up your answer yet, but I would. – Jagst3r15 Aug 9 '12 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.