• Hello,

    I’m having difficulties adding a character limited for an excerpt.
    I want to limit the character length of an excerpt that appears on a page template while keeping the default length (I believe its 55 characters) on other pages.

    I’ve used:

    function custom_excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    But this doesn’t seem to work.

    Any help much appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi rikardo85,

    You can try using some conditional tags along with the filter to change the excerpt length on different pages.

    Here is an example:

    function my_custom_excerpt_length( $default ) {
        if( is_home() ) {
            return 15;
        } elseif( is_single() ) {
            return 10;
        }
        return $default;
    }
    add_filter( 'excerpt_length', 'my_custom_excerpt_length' );

    The default excerpt length is 55 words. You can find out more about conditional tags here.

    Hope this is useful!

    Thread Starter rikardo85

    (@rikardo85)

    Hi redrambles,

    Thanks for your suggestions.

    I found a simpler solution by using:

    <?php echo substr(get_the_excerpt(), 0,30); ?>

    The above selects the character amount which is good enough for me.

    Thanks again

    Glad you found a solution and thank you for sharing it!

    Have a great day!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add character limit to excerpt filter’ is closed to new replies.