简体   繁体   English

Wordpress - 使用来自 URL 的图像作为帖子缩略图

[英]Wordpress - Use image from URL as a post thumbnail

I want to use an external image from URL as a post thumbnail, BUT I don't want to download and store the image in my server我想使用来自 URL 的外部图像作为后期缩略图,但我不想下载图像并将其存储在我的服务器中

I'm found this code in other guy question:我在其他人的问题中找到了这段代码:


        $filename = "thumbnail_".$post_id".jpg";
        $upload_file = wp_upload_bits( $filename, null, @file_get_contents('http://example.com/image.jpg') );

        if ( ! $upload_file['error'] ) {
          $filename = $upload_file['file'];
          $wp_filetype = wp_check_filetype($filename, null );

          $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_parent'    => $post_id,
            'post_title'     => preg_replace( '/\.[^.]+$/', '', $filename ),
            'post_content'   => '',
            'post_status'    => 'inherit'
          );

          $attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
          file_put_contents('attachment_id.txt', print_r($attachment_id, true));

          if ( ! is_wp_error( $attachment_id ) ) {
              require_once(ABSPATH . 'wp-admin/includes/image.php');

              $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
              wp_update_attachment_metadata( $attachment_id, $attachment_data );
              set_post_thumbnail( $post_id, $attachment_id );
          }
        }

It downloads the image to my server and also duplicates the image file in my uploads directory (I don't know why)它将图像下载到我的服务器并在我的上传目录中复制图像文件(我不知道为什么)

But I don't wanna download the image how can I achieve this?但我不想下载图像我怎么能做到这一点?

Assuming this image is just alternative to the featured image - you could store the URL of the image source in the custom field.假设此图像只是特色图像的替代品 - 您可以将图像源的 URL 存储在自定义字段中。

add_post_meta( $post_id, 'image', 'http://example.com/image.jpg' );

Then you can retrieve this URL anywhere you need to.然后,您可以在任何需要的地方检索此 URL。

I haven't tested this but assuming you're always going to want the same file, this may help get you started:我没有对此进行测试,但假设您总是想要相同的文件,这可能有助于您入门:

$filepath = 'http://example.com/'; //Change this to the actual file path...
$filename = 'image.jpg'; //Change this to the actual file name...
$filetype = wp_check_filetype( basename( $filename ), null );
$attachment = array(
    'guid'              => $filepath . $filename,
    'post_mime_type'    => $filetype['type'],
    'post_parent'       => $post_id,
    'post_title'        => preg_replace( '/\.[^.]+$/', '', $filename ),
    'post_content'      => '',
    'post_status'       => 'inherit'
);
$attachment_id = wp_insert_attachment( $attachment, $filename, $post_id );
if( !is_wp_error( $attachment_id ) ) {
    require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
    wp_update_attachment_metadata( $attachment_id, $attachment_data );
    set_post_thumbnail( $post_id, $attachment_id );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM