如果选中或未选中复选框,我可以为帖子设置缩略图。我遇到的问题是,如果选中复选框,然后取消选中,如何删除已设置的特征图像?一、 E.删除特色图像。我试着set_post_thumbnail($post->ID, \'\');
使用“”空白来删除图像,但这不起作用。
我的函数中的代码。php:
function post_extra_save( $post_id, $post){
if ( has_blocks( $post->post_content ) ) { // get blocks
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
if ( $block[\'blockName\'] === \'acf/opby-cover-image\' ) { // name of block
$media_url_thumb = $block[\'attrs\'][\'data\'][\'image_post\']; // Image ID from the block
$check_if_featured = $block[\'attrs\'][\'data\'][\'set_featured_image\'][0]; // check if checkbox is selected, will say \'sfi\' is enabled
if ($check_if_featured == \'sfi\') { // if checkbox is selected
set_post_thumbnail($post->ID, $media_url_thumb);
} else { // if checkbox is not selected
set_post_thumbnail($post->ID, \'\');
}
}
}
};
}
add_action( \'save_post\', \'post_extra_save\', 10, 2 );
最合适的回答,由SO网友:Sally CJ 整理而成
不,你不能使用set_post_thumbnail()
删除帖子缩略图。您需要的功能是delete_post_thumbnail()
.
// Both of these work.
delete_post_thumbnail( $post->ID ); // pass the post ID
//delete_post_thumbnail( $post ); // or post object