admin管理员组文章数量:1130349
Is there any way to use default_content only for a certain post type, specifically product (WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() ) or even if ( 'page' == get_post_type() ) to test it but it's not working.
Is there any way to use default_content only for a certain post type, specifically product (WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() ) or even if ( 'page' == get_post_type() ) to test it but it's not working.
1 Answer
Reset to default 1default_content is a filter used in the backend. You don't necessarily have anything in the loop so standard functions will probably fail. However, you're given a second argument of type WP_Post. You can check its post_type easily and work from there.
add_filter('default_content', 'WPSE_product_default_content', 10, 2);
function WPSE_product_default_content($post_content, $post) {
if ($post->post_type !== 'product')
return $post_content;
$content ='content to add to a post'
return $content;
}
Is there any way to use default_content only for a certain post type, specifically product (WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() ) or even if ( 'page' == get_post_type() ) to test it but it's not working.
Is there any way to use default_content only for a certain post type, specifically product (WooCommerce)?
My code:
add_filter( 'default_content', 'set_default_content', 10, 2 );
function set_default_content( $content, $product ) {
$content ='content to add to a post']';
return $content;
}
I've tried with if ( 'product' == get_post_type() ) or even if ( 'page' == get_post_type() ) to test it but it's not working.
1 Answer
Reset to default 1default_content is a filter used in the backend. You don't necessarily have anything in the loop so standard functions will probably fail. However, you're given a second argument of type WP_Post. You can check its post_type easily and work from there.
add_filter('default_content', 'WPSE_product_default_content', 10, 2);
function WPSE_product_default_content($post_content, $post) {
if ($post->post_type !== 'product')
return $post_content;
$content ='content to add to a post'
return $content;
}
本文标签: woocommerce offtopicFilter defaultcontent only for products
版权声明:本文标题:woocommerce offtopic - Filter default_content only for products 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749147628a2323266.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论