2

Hello I need help customizing the excerpt length for specific pages.

For the homepage excerpts I want 400 characters and for all other pages I want 800 characters.

I came up with the following code placed in functions.php

function wpdocs_custom_excerpt_length( $length ) {
    if( is_front_page() ){
        return 400;
    } else {
        return 800;
    }
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

Unfortunately this doesnt seem to work

Anyone got a solution for this?

Thanks!

  • Have you tried using is_home() instead of is_front_page()? – Jack Johansson Feb 12 '17 at 11:16
  • Yup i tried that – cup_of Feb 12 '17 at 11:19
  • @JackJohansson u are right sir! Turns out my function was correct the whole time I just mixed up characters and words. Thanks! – cup_of Feb 12 '17 at 11:29
2

The excerpt length is the number of words, not the characters. Assuming that every word is 8 characters long in average, you might want to use this:

function wpdocs_custom_excerpt_length( $length ) {
    if( is_front_page() ){
        return 50;
    } else {
        return 100;
    }
}
add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

Source : WordPress codex

  • 1
    Thanks! Such a simple fix but I wouldnt of gotten it without your help. – cup_of Feb 12 '17 at 11:29

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.