我最喜欢的处理此问题的方法是使用我在另一篇堆栈文章中发现的一个有文档记录的函数:media_sideload_image
它的工作原理是将图像url提取到WordPress上载目录,然后将图像与帖子的附件相关联。
您可以这样尝试:
// required libraries for media_sideload_image
require_once(ABSPATH . \'wp-admin/includes/file.php\');
require_once(ABSPATH . \'wp-admin/includes/media.php\');
require_once(ABSPATH . \'wp-admin/includes/image.php\');
// $post_id == the post you want the image to be attached to
// $video_thumb_url == the vimeo video\'s thumb url
// $description == optional description
// load the image
$result = media_sideload_image($video_thumb_url, $post_id, $description);
// then find the last image added to the post attachments
$attachments = get_posts(array(\'numberposts\' => \'1\', \'post_parent\' => $post_id, \'post_type\' => \'attachment\', \'post_mime_type\' => \'image\', \'order\' => \'ASC\'));
if(sizeof($attachments) > 0){
// set image as the post thumbnail
set_post_thumbnail($post_id, $attachments[0]->ID);
}