Json API plugin- Get thumbnail image from Attachments Array

I am pulling in data from WordPress using Json API plugin. I am successfully getting the array of attachments using:

var myImage=myJSON[j].attachments;

But that gives me the whole array. How do I get just the thumbnail image from inside that array?

Related posts

Leave a Reply

2 comments

  1. If you want the post thumbnail url for the post “featured image” you can just do:

    var myImage=myJSON[j].thumbnail_images.thumbnail.url;
    

    If you want to get the thumbnail of any attachments URL do this:

    var myImage=myJSON[j].attachments[0].images.thumbnail.url;
    

    just replace the “[0]” with the index of the image you want to get. console.log() and check your JSON response to find the index of the desired image.

  2. With the current version of this plugin, this works as kmazla described only if you upload the image while editing the post/or creating the post for the first time. If you attach an existing image from the media database, it will still show up as an empty array in the query. If you decide to upload more photos (since the first one you uploaded was as mistake for example) they get appended to the attachments array even though you only select one of the images as your “featured image”. It is quite confusing.

    The workaround?

    Make a fresh new post and make sure you attach the right images in the right sequence. Older posts should be deleted and republished for the attachments array to be lined up properly.