apply_filters( 'excerpt_length', int $number )

Filters the number of words in an excerpt.


Description Description


Parameters Parameters

$number

(int) The number of words. Default 55.


Top ↑

Source Source

File: wp-includes/formatting.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

More Information 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.



Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by acosmin

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

    prefix_custom_excerpt_length( $length ) {
    	$custom = get_theme_mod( 'custom_excerpt_length' );
    	if( $custom != '' ) {
    		return $length = intval( $custom );
    	} else {
    		return $length;
    	}
    }
    add_filter( 'excerpt_length', 'prefix_custom_excerpt_length', 999 );
    
  2. Skip to note 2 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.

  3. Skip to note 3 content
    Contributed by Metallicarosetail
    /**
     * 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.

  4. Skip to note 4 content
    Contributed by prashaddey
    /**
     * 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; } );

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