将外部 URL 中的图像设置为特色图像(如果不存在)


set image from external url as featured image if its not present

如果我单击发布或更新时没有特色图像,我正在尝试将已经上传到网站/媒体库中的备用图像设置为特色图像,但它没有设置图像。 什么也没发生

函数中的代码.php

function thumb_check($post_id){    
$url="http://example.com/uploads/flower.png";    
if (has_post_thumbnail( $post_id)){}    
else { update_post_meta($post_id,'_thumbnail_id',$url);}    
}
add_action( 'save_post', 'thumb_check' );

_thumbnail_id仅适用于WP照片ID而不是URL。看看这篇文章 https://wordpress.org/support/topic/_thumbnail_id-not-display-when-full-url-of-image

这是我的建议:

更新:url_to_postid似乎对我来说效果不佳。您应该转到媒体,然后查找您的回退映像 ID,并在下面的代码中手动放置映像 ID。要查看图像 ID,请查看 url,它应如下所示 http://example.com/wp-admin/upload.php?item=51 其中 51 是图像 ID。另外,我用set_post_thumbnail替换了update_post_meta

函数.php

function thumb_check($post_id) {
    if(!has_post_thumbnail($post_id)) {
        set_post_thumbnail($post_id, 51)
    }
}
add_action('save_post', 'thumb_check');