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.
Source Source
Changelog Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
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 );/** * 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.
/** * 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:-
This will display your post excerpt up to 30 words.
/** * Filter the excerpt length to 20 characters. * * @param int $length Excerpt length. * @return int (Maybe) modified excerpt length. */ add_filter( 'excerpt_length', function( $length ) { return 20; } );