apply_filters( 'excerpt_length', int $number )
Filters the maximum number of words in a post excerpt.
Description
Parameters
- $number
-
(int) The maximum number of words. Default 55.
Source
Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
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.
Set the excerpt length based on a theme mod, through the Customizer.
This will help to apply the excerpt_length on front end only.
Feedback
It’s important to set the priority to a high number so your callback is not overwritten by the WordPress default callback. — By leogermani —
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.