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.
- how do I add a default excerpt value to all new posts of a custom post type?
- 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.
- how do I add a default excerpt value to all new posts of a custom post type?
- 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 Answer
Reset to default 1This 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
版权声明:本文标题:functions - Default excerpt for parent of a custom post type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742127102a2421991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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