Different Excerpt Types for Custom Post Type in WordPress

By default, you can only use or display one excerpt type in WordPress. It does not matter if the archive page is for the default post type or for a custom post type. In this tutorial, you will learn how to use a different excerpt type for the default post type and for a custom post type.

Copy the two groups of code below to the functions.php file of your WordPress website. Make sure you change the word “tutorials” to the name of your custom post type.

Code 1 – Excerpt Type for Custom Post Type

// Truncate excerpts and add 'Read More' for Custom Post Type: Tutorials
// Truncate excerpt
function custom_excerpt_length( $length ) {
    global $post;
    if ($post->post_type == 'tutorials') {
return 80; // Number of words to be displayed
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// Add a "Read More" link to the end of an excerpt
function excerpt_read_more( $more ) {
    global $post;
    if ($post->post_type == 'tutorials') {
return '... <a href="'.get_the_permalink().'" class="readmore">Read More</a>';
add_filter( 'excerpt_more', 'excerpt_read_more' );

Code 1 will display a certain number of words as excerpts in archives for the custom post type “tutorials”. Each excerpt ends with the phrase “Read More”, which links to the actual post.

Code 2 – Excerpt Type for Default Post Type

// Display first paragraphs as excerpts for Default Post Type
 function wp_custom_excerpt($text, $raw_excerpt) {
    global $post;
    if(( ! $raw_excerpt ) and ($post->post_type == 'post') ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    $text = preg_replace("/<img[^>]+\>/i", "", $text); 
    return $text;
add_filter( 'wp_trim_excerpt', 'wp_custom_excerpt', 10, 2 );

Code 2 will display first paragraphs as excerpts in archives for the default post type.