Wordpress - 使用 URL 中的图像作为 post 缩略图
Wordpress - Use image from URL as a post thumbnail
我想使用来自 URL 的外部图像作为 post 缩略图,但是 我不想下载图像并将其存储在我的服务器中
我在其他人的问题中找到了这段代码:
$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 );
}
}
它将图像下载到我的服务器并在我的上传目录中复制图像文件(我不知道为什么)
但是我不想下载图片怎么办?
我没有对此进行测试,但假设您总是想要同一个文件,这可能有助于您入门:
$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 );
}
假设此图片只是特色图片的替代图片 - 您可以将图片来源的 URL 存储在自定义字段中。
add_post_meta( $post_id, 'image', 'http://example.com/image.jpg' );
然后您可以在任何需要的地方检索此 URL。
我想使用来自 URL 的外部图像作为 post 缩略图,但是 我不想下载图像并将其存储在我的服务器中
我在其他人的问题中找到了这段代码:
$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 );
}
}
它将图像下载到我的服务器并在我的上传目录中复制图像文件(我不知道为什么)
但是我不想下载图片怎么办?
我没有对此进行测试,但假设您总是想要同一个文件,这可能有助于您入门:
$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 );
}
假设此图片只是特色图片的替代图片 - 您可以将图片来源的 URL 存储在自定义字段中。
add_post_meta( $post_id, 'image', 'http://example.com/image.jpg' );
然后您可以在任何需要的地方检索此 URL。