If you have some custom post type either created using a WordPress plugin or a custom code that you have written, then you might want to change the excerpt length of the custom posts that are displayed in the archives or category pages.
Changing excerpt length of custom post types
Add the code below to the functions.php file of your WordPress child theme. What the code does is that it looks for the custom post type “events-cat“, then returns the value “25“. The value 25 limits the excerpt length to 25 words.
You can modify the value and increase or decrease the excerpt length to the number of words that you want,
//limit excerpt length code
function mycustom_excerpt_length($length) {
global $post;
if ($post->post_type == ‘event-cat’)
return 25;
else
return 55;
}
add_filter(‘excerpt_length’, ‘mycustom_excerpt_length’);
NOTE 1: The else statement return value of 55 represents the default excerpt length of other posts (that are not custom posts). You can change the length to the number of words that you want.
NOTE 2: The home page and all other archive pages that are not part of the custom post type will use the length returned by the else statement (that is 55 words). If you want posts on the home or frontpage to display a different length, then add the code below before the else statement
else if (is_home())
return 13;
You can modify the number of words from 13 to what you want.
Leave a Reply