apply_filters( ‘excerpt_length’, int $number )

Filters the maximum number of words in a post excerpt.

Parameters

$numberint
The maximum number of words. Default 55.

More Information

Use this filter if you want to change the default excerpt length.

To change excerpt length, add the following code to functions.php file in your theme adjusting the “20” to match the number of words you wish to display in the excerpt:

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

Make sure to set the priority correctly, such as 999, otherwise the default WordPress filter on this function will run last and override what you set here.

Source

$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 6 content
    /**
     * Filter the excerpt length to 50 words.
     *
     * @param int $length Excerpt length.
     * @return int (Maybe) modified excerpt length.
     */
    function theme_slug_excerpt_length( $length ) {
            if ( is_admin() ) {
                    return $length;
            }
            return 50;
    }
    add_filter( 'excerpt_length', 'theme_slug_excerpt_length', 999 );

    This will help to apply the excerpt_length on front end only.

  2. Skip to note 7 content

    Set the excerpt length based on a theme mod, through the Customizer.

    function prefix_custom_excerpt_length( $length ) {
    	$custom = get_theme_mod( 'custom_excerpt_length' );
    
    	if ( '' !== $custom ) {
    		return absint( $custom );
    	} else {
    		return $length;
    	}
    }
    add_filter( 'excerpt_length', 'prefix_custom_excerpt_length', 999 );
  3. Skip to note 8 content
    /**
     * Filter the excerpt length to 20 words.
     *
     * @param int $length Excerpt length.
     * @return int (Maybe) modified excerpt length.
     */
    add_filter( 'excerpt_length', function( $length ) { return 20; } );
  4. Skip to note 9 content
    /**
     * Filter the excerpt length to 30 words.
     *
     * @param int $length Excerpt length.
     * @return int (Maybe) modified excerpt length.
     */
    function wp_example_excerpt_length( $length ) {
        return 30;
    }
    add_filter( 'excerpt_length', 'wp_example_excerpt_length');

    Notes: This will display your excerpt text up to 30 words using excerpt_length filter. By default the number of words is 55.

    Example:-

    <?php the_excerpt(); ?>

    This will display your post excerpt up to 30 words.

  5. Skip to note 10 content
    /**
     * Filter the excerpt length to 80 words on homepage and 50 words on page template
     *
     * @param int $length Excerpt length.
     * @return int modified excerpt length.
     */
    function wpdocs_custom_excerpt_length( $length ) {
        if ( is_page_template( 'page-templates/services-page.php' ) ) {
            return 50;
        } else if ( is_front_page() && is_home() ) {
            return 80;
        } else {
            return $length;
        }
    }
    
    add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

    This is useful to display different excerpt length on homepage, custom page template (where listing some custom post types).

You must log in before being able to contribute a note or feedback.