admin管理员组

文章数量:1323714

I've created a hierarchical post type called mycustomposttype. Parents are created in the backend, children via gravityforms.

Due to my theme setup, I would like all parents in mycustomposttype to display the shortcode [mycustomshortcode] in the excerpt.

To start, I've tried to auto-populate the excerpt for all mycustomposttype posts.

add_filter( 'get_the_excerpt', function( $post_excerpt, $post ){
if( $post->post_type != 'mycustomposttype' ) 
return $post_excerpt; 
return '[mycustomshortcode]';
 }, 99, 2 );

This does not produce any excerpt when creating or editing posts.

  1. how do I add a default excerpt value to all new posts of a custom post type?
  2. how do I add the default value to only all new parents of that custom post type?

Thanks.

I've created a hierarchical post type called mycustomposttype. Parents are created in the backend, children via gravityforms.

Due to my theme setup, I would like all parents in mycustomposttype to display the shortcode [mycustomshortcode] in the excerpt.

To start, I've tried to auto-populate the excerpt for all mycustomposttype posts.

add_filter( 'get_the_excerpt', function( $post_excerpt, $post ){
if( $post->post_type != 'mycustomposttype' ) 
return $post_excerpt; 
return '[mycustomshortcode]';
 }, 99, 2 );

This does not produce any excerpt when creating or editing posts.

  1. how do I add a default excerpt value to all new posts of a custom post type?
  2. how do I add the default value to only all new parents of that custom post type?

Thanks.

Share Improve this question asked Sep 7, 2020 at 22:43 dkrahldkrahl 1 2
  • 1 If you're using a custom shortcode would it not be easier to just call the function that implements the shortcode directly? Why bother with the shortcode at all if it's just going to get parsed and turned back into the function call anyway. It's wasteful, similar to writing down notes for yourself by putting them in the mail and waiting for them to be delivered back to you. Note that get_the_excerpt only runs at runtime, not on update/save and has no impact on the editor – Tom J Nowell Commented Sep 7, 2020 at 22:51
  • @dkrahl check the response this should resolve your issue. Don't forget to let us know if it works and to accept answer if it does (so others can benefit as well) – sMyles Commented Sep 13, 2020 at 23:09
Add a comment  | 

1 Answer 1

Reset to default 1

This will default the excerpt value in your custom post type only for new posts:

add_filter( 'default_excerpt', 'smyles_default_custom_post_excerpt', 10, 2 );

/**
 * Default Excerpt for Custom Post Type
 *
 * @param string  $post_excerpt Default post excerpt.
 * @param WP_Post $post         Post object.
 *
 * @return string
 *
 */
function smyles_default_custom_post_excerpt( $post_excerpt, $post ){
    
    if( $post && $post->post_type === 'mycustomposttype' && ! $post->post_parent ){
        return '[mycustomshortcode]';
    }
    
    return $post_excerpt;
}

The check for $post->post_parent checks the parent, if it is a "child" post, the value will be different than 0 which means it's the parent post

本文标签: functionsDefault excerpt for parent of a custom post type