如何获得一个帖子缩略图与WP卸载S3 Wordpress插件


How to get a post thumbnail with the WP Offload S3 Wordpress plugin

对于我在WP网站上的帖子,我使用此代码来获取缩略图的URL:

$thumb_id = get_post_thumbnail_id($single->ID);
$thumb_url = get_guid($thumb_id);

我已经安装了WP卸载S3插件,但我的代码返回了本地文件url,而不是S3 url。

你能帮我吗?

我终于找到了在数据库中搜索的方法。我在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;
}

如果有一天有人需要,我会发布我的功能。

如果您有附件ID,请尝试以下操作:

wp_get_attachment_url( $attachment->ID )

这将为您提供S3 URL。