Here’s how to have different excerpt lengths for different post types. This example sets lengths for regular posts (line 5), and custom post types named ‘products’ (line 7) and ‘testimonial’ (line 9). Choose the length of words on lines 6, 8, 10, and 12. Line 12 is the default excerpt length for all other post types.
// CHANGE EXCERPT LENGTH FOR DIFFERENT POST TYPES function isacustom_excerpt_length($length) { global $post; if ($post->post_type == 'post') return 32; else if ($post->post_type == 'products') return 65; else if ($post->post_type == 'testimonial') return 75; else return 80; } add_filter('excerpt_length', 'isacustom_excerpt_length');
For a simpler function to just choose the excerpt length for regular posts, see:
sarat
February 8th, 2013 at 2:25 pm
Thanks for this snippet. It was helpful to give different names for “read more button” based on post type.
function new_excerpt_more($more) {
global $post;
if ($post->post_type == ‘post’)
return ‘… ID) . ‘”>Full Story‘;
else if ($post->post_type == ‘custom_type’)
return ‘… ID) . ‘”>Biography and Articles‘;
}
add_filter(‘excerpt_more’, ‘new_excerpt_more’);
Kingsley
May 16th, 2014 at 3:33 am
Hello, thanks do you have any idea how to change the read more text for a custom post type?
Isabel
May 25th, 2014 at 5:00 pm
Please see: Custom Excerpt “Read More” Link for Custom Post Types
Dante
May 29th, 2014 at 9:06 pm
Thanks thanks thanks! I was more than one hour searching by a function like that, thanks Isa.
Serge Zahharenko
December 15th, 2014 at 2:54 pm
Thanks alot!
B M Rafiul Alam
February 28th, 2016 at 12:16 am
Thanks, that’s perfect 🙂
Cliff
March 9th, 2016 at 2:15 am
Thanks for your help.
This is exactly what I was looking for!
Dan
February 20th, 2018 at 12:15 pm
Thanks Isabel!
Any idea how to set a different length of an excerpt when the post type is on the homepage?
I tried this but it didn’t work:
Isabel
February 21st, 2018 at 12:58 pm
Just move lines 5 and 6 down below line 9. Check for ‘jetpack-testimonial’ on front page first, then all other ‘jetpack-testimonial’ get 85.
Dan
February 22nd, 2018 at 10:21 am
Thanks Isabel,
I think it’s not working because I’m using a shortcode to display the testimonials in a ‘normal’ page.
Will try to see a different option for that.
Thanks again,
Dan