Skip to content
This repository has been archived by the owner. It is now read-only.

Displaying featured images #1558

Closed
scottopolis opened this issue Sep 16, 2015 · 8 comments

Comments

@scottopolis
Copy link

commented Sep 16, 2015

Just wondering if I'm missing something here.

The 1.x way of getting a post's featured image url in javascript was like this:

postObject.featured_image.attachment_meta.sizes.thumbnail.url

Easy peasy.

The 2.x way requires you to make your api call with ?_embed, and then drill down in the post object like this:

postObject._embedded["http://v2.wp-api.org/attachment"][0][0].source_url

Is there an easier way to do this? It seems unnecessarily complex to me.

Just curious. Thanks guys!

@bigandy

This comment has been minimized.

Copy link

commented Sep 17, 2015

You can use the rest_prepare_post filter like so. Note that I am getting all additional image sizes:

function ah_rest_prepare_post( $data, $post, $request ) {
    global $_wp_additional_image_sizes;

    if ( empty( $_wp_additional_image_sizes ) ) {
        return $sizes;
    }
    $_data = $data->data;
    $thumbnail_id = get_post_thumbnail_id( $post->ID );

    $featured_image_urls = [];
    foreach ( $_wp_additional_image_sizes as $size => $value ) {
        $thumbnail_info = wp_get_attachment_image_src( $thumbnail_id, $size );

        $featured_image_urls[ $size ] = $thumbnail_info[0];
    }

    $_data['featured_image_full_urls'] = $featured_image_urls;
    $data->data = $_data;
    return $data;
}
add_filter( 'rest_prepare_post', 'ah_rest_prepare_post', 10, 3 );

resulting in the following output :

"featured_image_full_urls": {
"pic_max": "https://big-andy.co.uk/content/uploads/2015/08/banner-1544x500-1400x453.jpg",
"pic_large": "https://big-andy.co.uk/content/uploads/2015/08/banner-1544x500-1000x324.jpg",
"pic_medium": "https://big-andy.co.uk/content/uploads/2015/08/banner-1544x500-750x243.jpg",
"pic_small": "https://big-andy.co.uk/content/uploads/2015/08/banner-1544x500-500x162.jpg"
},

full output response object

@scottopolis

This comment has been minimized.

Copy link
Author

commented Sep 17, 2015

@bigandy thanks for that, but I was more wondering about the philosophy behind this method in core, and if I could persuade them to make it easier to retrieve post images.

@scottopolis

This comment has been minimized.

Copy link
Author

commented Sep 17, 2015

It also looks like post thumbnails are not in the _embedded object. I'm not sure how we are supposed to get a list of posts with thumbnails using one API call.

@scottopolis

This comment has been minimized.

Copy link
Author

commented Sep 17, 2015

Another update, when I use the ?_embed flag on a single post, it gives me the wrong attachments. For example this post: https://apppresser.com/wp-json/wp/v2/posts/49053?_embed

The attachments in _embedded["http://v2.wp-api.org/attachment"] are not associated with that post at all.

Is this a bug?

@rmccue

This comment has been minimized.

Copy link
Member

commented Sep 18, 2015

The idea behind this is reducing duplication of data; _embedded is for clients with high latency.

We do want to make these a bit easier to access, see #1488. As for the association problem, see #1476.

@rmccue rmccue closed this Sep 18, 2015

@abdusfauzi

This comment has been minimized.

Copy link

commented Dec 4, 2015

Glad that I finally found this thread. Was about to hack the plugin and worry about future updates. Not in the v2 Docs?

@BackPackerDz

This comment has been minimized.

Copy link

commented Dec 14, 2015

Same problem, why it's too complicate ?

@fobar21

This comment has been minimized.

Copy link

commented Apr 14, 2016

Dear, its works with post, but not for cusom post type? Can i get featured image url from cpt?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants
You can’t perform that action at this time.