Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
    
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

2 Answers 2

up vote 6 down vote accepted

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;
}
share|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

kaiser gave an excellent answer so I'm just adding this for general info.

There is another way to approach this as well. If you need the use for multiple excerpts, you can just make use of wp-trim_words. You just need to replace the_excerpt(); with echo custom_excerpts($limit); where $limit is the amount of words.

function custom_excerpts($limit) {
    return wp_trim_words(get_the_excerpt(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a>');
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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