Search code examples
jqueryarraysjsonapitumblr

Tumblr API v2: Displaying Images?


I am trying to set up the Tumblr API on my site.

So far I have text posts working fine like such:

$.ajax({
    url: "http://api.tumblr.com/v2/blog/myblog.tumblr.com/posts?api_key=myapikey",
    dataType: 'jsonp',
    success: function(results){

     var i = 0;

     while (i < 20) {

       var type = results.response.posts[i].type;
       var date = results.response.posts[i].date;

       if (type == "text") {
         var title = results.response.posts[i].title;
         var content = results.response.posts[i].body;
         $("#myDivId").append("<div class='posttitle'><h2>" + title + "</h2></div>");
         $("#myDivId").append("<div class='postbody'>" + content + "</div>");
       }
    i++;
     }//END WHILE

    }//END RESULTS FUNCTION
});

But I cannot get the images to work. I know that, for whatever reason, images in a post are held within an array so I thought doing the following would suffice to get the first image:

   else if (type == "photo") {
         var photourl = results.response.posts[i].photos[0].url;
         $("#myDivId").append("<div class='postbody'><img src='" + photourl + "'/></div>");
       }

But no avail. The Docs are here if anyone is interested: http://www.tumblr.com/docs/en/api/v2#photo-posts

Would anyone know how to get these images working? Thanks


Solution

  • Looks like it's actually in alt_sizes:

    results.response.posts[i].photos[0].alt_sizes[i].url
    

    http://jsfiddle.net/ExplosionPIlls/WQqLC/