How to make a default excerpt for custom post type

I would like to ask you how can I create a default except value (text, content) for all post of certain custom post type. Eg: Post1 has excerpt: "Check this post .... read more" Post2 has the same excerpt etc etc. Is that possible?

  • Ash

    Hello Stefan

    If your theme used default wordpress' excerpt function, then the following code should work:

    add_filter( 'get_the_excerpt', function( $post_excerpt, $post ) {
    	if( $post->post_type != 'XXXX' ) return $post_excerpt;
    	return 'Check this post .... <a href="' . get_permalink( $post->ID ) . '">read more</a>';
    }, 99, 2 );

    Replace XXXX with the custom post type name that you want to change excerpt to.

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    If your theme used custom function, then you have to contact them if there is any hook that we can use to replace the excerpt content.

    Have a nice day!


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.