I have some code which currently loops through posts on a blog and outputs them as inside a list item as an image with link and caption text if it exists. This works fine, but when I run the code and there are photoset posts each image and caption gets looped as per the photo blog posts.
(So if there are 10 images in a photoset and 1 caption each image and caption gets output 10 times).
Here is my original code which works:
// get tumblr posts and embed them.
var postContent = $('#tumblr-posts');
var getPosts = {};
getPosts.get = function(){
var offSet = href * 10;
$.ajax({
url:"http://api.tumblr.com/v2/blog/jessicaharby.tumblr.com/posts?limit=11",
dataType: 'jsonp',
data: {
api_key: _site._apiKey,
tag: 'news',
offset: offSet
},
success: function(results){
var i = 0;
while (i < results.response.posts.length) {
var type = results.response.posts[i].type;
var photoset = results.response.posts[i].photoset_layout;
if (type == "photo") {
var photos = results.response.posts[i].photos;
var linkURL = results.response.posts[i].post_url;
var caption = results.response.posts[i].caption;
for (var j = 0; j < photos.length; j++) {
var imgURL;
if (photos[j].alt_sizes[1]) {
imgURL = photos[j].alt_sizes[1].url
}else{
continue;
}
postContent.append("<li><img src=" + imgURL + " /><p>" + caption + "</p></li>");
}
i++;
}
console.log(results.response);
postContent.find('a').attr('target','_blank');
}
});
}
getPosts.get();
I have been trying to write an else if statement which captures the photoset posts. It needs refining but looks like this:
else if(photoset != null) {
for (var k = 0; k < photos.length; k++) {
var imgURL;
if (photos[k].alt_sizes[1]) {
imgURL = photos[k].alt_sizes[1].url
}else{
continue;
}
postContent.append("<li><img src=" + imgURL + " /></li>");
}
postContent.append("<li><p>" + caption + "</p></li>");
}else{
console.log('some error')
}
So it should spit out all the images and the one caption at the bottom of the post (so I guess I need these all inside one list item). I am just not 100% sure how to finalise the loops.
Another issue is that if I write an if/else if I think with my current code the first if is true and so the posts will be repeated potentially, or the else if may be ignored.
I have two fiddles which can be experimented with/forked etc;
1) http://jsfiddle.net/lharby/bLveggwk/1/ loops through all posts
2) http://jsfiddle.net/lharby/bLveggwk/3/ only loops through photoset posts.
Apologies that this is not the neatest question. If I am not clear I can make a drawing if that helps!
EDIT
Decided to make an image:
I'm not sure I understood the problem well, btw this solution http://jsfiddle.net/1bLrocj1/ works?
significant part:
var set = [];
while (i < results.response.posts.length) {
var type = results.response.posts[i].type;
var photoset = results.response.posts[i].photoset_layout;
if (photoset != null) {
var photos = results.response.posts[i].photos;
var linkURL = results.response.posts[i].post_url;
var caption = results.response.posts[i].caption;
for (var j = 0; j < photos.length; j++) {
var imgURL;
if (photos[j].alt_sizes[1]) {
imgURL = photos[j].alt_sizes[1].url
} else {
continue;
}
set.push("<li><img src=" + imgURL + " /></li>");
}
if (caption) {
set.push("<li><p>" + caption + "</p></li>");
var subUl = document.createElement("ul");
for (var k = 0; k < set.length; k++) {
$(subUl).append(set[k]);
}
postContent.append(subUl);
set.length = 0;
}
} else {
console.log('some error')
}
i++;
}
css:
ul li {
padding:20px;
float:left;
max-width:550px;
background:#FFF;
}
ul li:last-child {
margin:0 1px 30px 0;
}