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 ↑

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 characters.
     *
     * @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 characters.
     *
     * @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.

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