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:
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.
/**
* Filter the excerpt length to 80 words on homepage and 50 words on page template
*
* @paramint$length Excerpt length.
* @returnint modified excerpt length.
*/functionwpdocs_custom_excerpt_length($length){if(is_page_template('page-templates/services-page.php')){return50;}elseif(is_front_page()&&is_home()){return80;}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).
wp-includes/formatting.php
This will help to apply the excerpt_length on front end only.
Set the excerpt length based on a theme mod, through the Customizer.
wp-includes/formatting.php
wp-includes/formatting.php
wp-includes/formatting.php
Notes: This will display your excerpt text up to 30 words using excerpt_length filter. By default the number of words is 55.
Example:-
wp-includes/formatting.php
This will display your post excerpt up to 30 words.
wp-includes/formatting.php
This is useful to display different excerpt length on homepage, custom page template (where listing some custom post types).