简体   繁体   中英

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

for my posts in my WP website, I use this code to get the URL of the thumbnail :

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

I've installed the WP Offload S3 plugin, but my code returned the local file url and not the S3 url.

Can you help me ?

I finally find a way in searching in the database. I find a data in the postmeta table, "amazonS3_info", with all the infos I need to generate the file 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;
}

I post my function if anyone need it someday.

If you have the attachment ID, try this instead:

wp_get_attachment_url( $attachment->ID )

This will give you the S3 URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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