SET_POST_THMBIAN是否可用于删除缩略图?

时间:2020-10-12 作者:Gregory Schultz

如果选中或未选中复选框,我可以为帖子设置缩略图。我遇到的问题是,如果选中复选框,然后取消选中,如何删除已设置的特征图像?一、 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 );

1 个回复
最合适的回答,由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

相关推荐

Functions.php上未定义$_GET和&_REQUEST索引

我最近尝试学习为我的自定义主题创建一个主题选项页面,这是按照stackoverflow和其他资源的教程进行的。但脚本显示错误if ($_GET[\'page\'] == basename(__FILE__)) { if (\'save\' == $_REQUEST[\'formaction\']) { foreach ($options as $value) { if( isset( $_REQUEST[ $value[\'id\']