【问题标题】:How to get a post thumbnail with the WP Offload S3 Wordpress plugin如何使用 WP Offload S3 Wordpress 插件获取帖子缩略图
【发布时间】:2016-08-07 10:34:52
【问题描述】:
对于我在 WP 网站上的帖子,我使用此代码获取缩略图的 URL:
$thumb_id = get_post_thumbnail_id($single->ID);
$thumb_url = get_guid($thumb_id);
我已经安装了 WP Offload S3 插件,但我的代码返回了本地文件 url 而不是 S3 url。
你能帮帮我吗?
【问题讨论】:
标签:
php
wordpress
amazon-s3
【解决方案1】:
我终于找到了在数据库中搜索的方法。我在 postmeta 表“amazonS3_info”中找到了一个数据,其中包含生成文件 URL 所需的所有信息:
function get_s3_thumb($post_id){
$thum_id = get_post_thumbnail_id($post_id);
$meta = get_post_meta($thum_id, 'amazonS3_info');
if(count($meta)){
// The file exist in S3
$meta = $meta[0];
$url = ($_SERVER['HTTPS'] == 'on')?'https':'http';
$url.= '://s3-';
$url.= $meta['region'];
$url.= '.amazonaws.com/';
$url.= $meta['bucket'];
$url.= '/';
$url.= $meta['key'];
}else{
// The file dosen't exist in S3
$url = get_guid($thum_id);
if($_SERVER['HTTPS'] == 'on'){
$url = str_replace('http', 'https', $url);
}
}
return $url;
}
如果有一天有人需要,我会发布我的功能。
【解决方案2】:
如果您有附件 ID,请尝试以下操作:
wp_get_attachment_url( $attachment->ID )
这将为您提供 S3 URL。