Question
what are examples of code that will display images on the page? looking for both the HTML and the javascript
Background
I was following along to a tutorial and was able to replicate their example, but now stuck in implementing for my own website
Code
Demo on Codepen: http://codepen.io/JGallardo/pen/jVRKGP
Javascript
this is what I am having a problem with
$.ajax({
url: "http://api.tumblr.com/v2/blog/typophile.tumblr.com/posts?api_key=sNCvOfqUTzUJzBOViCbYfkaGeQaFAS4Q4XNtHMu8YPo6No3OiY",
dataType: 'jsonp',
success: function(posts){
var postings = posts.response.posts;
console.log(postings);
var text = '';
for (var i in postings) {
var p = postings[i];
text += '<li><img src=' + p.photos[0].original_size.url +'><a href='+p.post_url+'>'+p.source_title+'</a></li>';
}
$('ul').append(text);
}
});
I copied that from a tutorial, so if I understand a few things, I can replicate or improve on for my use, namely
What is var p = postings[i];
I just want the images, and I want them to open in a new window, but this tutorial had
<li><img src=' + p.photos[0].original_size.url +'><a href='+p.post_url+'>'+p.source_title+'</a></li>
so i can clean that up to something like
<li><img src=' + p.photos[0].original_size.url +'><a href='+p.post_url+' target="_blank"></a></li>
Also in that url, when i swap it out with mine, it just does not work (display any images). See: http://api.tumblr.com/v2/blog/lolsnack.tumblr.com/posts?api_key=sNCvOfqUTzUJzBOViCbYfkaGeQaFAS4Q4XNtHMu8YPo6No3OiY
I tried a few random other blogs, and it works. including with fashion.tumblr.com
and kanye.tumblr.com
I should also note that I only want single images, i think their method was targeted at photosets?
You need to define the post type. As you mention in the example they are targeting photosets.
This code should work with photo posts (you will need to tinker it slightly):
EDIT
OK, you have a few issues which I will try and address.
I have made a jsfiddle and commented my code, but I will try to explain.
Firstly, change your api call from
http://
tohttps://
Secondly the blog you are calling is only returning photo posts, so the code you have so far works fine. The default number of posts is 20 but I set an upper limit of 50, and all posts are still in fact photo posts. So we cannot use it to test our code. See for example: http://typophile.tumblr.com/archive/filter-by/text
So I tried using my own blog and setting an upper limit of 50 posts.
Thirdly you want to wrap your images in an anchor and have them open in a new window? Currently you are outputing the image with an anchor afterwards and no text for the link.
So change this:
text += '<li><img src=' + p.photos[0].original_size.url +'><a href='+p.post_url+'>'+p.source_title+'</a></li>';
To this:
text += '<li><a href='+p.post_url+' target="_blank"><img src=' + p.photos[0].original_size.url +'></a></li>';
This is my code:
If you run this from jsfiddle and inspect the console you will currently see the attached image.
And if you look at the html output you can see that the majority are photo posts, which our code already accounts for, and now I have written some statements to allow for text posts, and other post types (a fallback really). If you want to specify different output for more post types you have to add them into the
if/else
if statement.To further test this I would try swapping the url parameter in the ajax call to try and test other blogs that will return lots of different post types.
I tried this
url: "https://api.tumblr.com/v2/blog/andthentheycalledmetania.tumblr.com/posts?limit=50&api_key=sNCvOfqUTzUJzBOViCbYfkaGeQaFAS4Q4XNtHMu8YPo6No3OiY",
And look at the post types returned.
Much better for testing.
I hope this explains the process better. If you have more questions feel free to ask.