Different Excerpt Length For Different Post Types

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:

Change the Length of the Excerpt in WordPress

See more:

We've 10 Responses

  1. 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:

    function twentyten_excerpt_length( $length ) {
    global $post;
    	 if ($post->post_type == 'post')
        return 20;
    	else if ($post->post_type == 'jetpack-testimonial')
        return 85;
    	
    	else if (is_front_page() &&	($post->post_type == 'jetpack-testimonial'))
    		 return 20;
    						  
    }
    
    add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
    
    
    Dan
  2. 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

    Dan

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]