Blogger: Fetch label post using JSON callback

625 views Asked by At

I try to fetch data using blogger json callback. With the following java script code, i can get title, link, thumb, summary, and label/category. Everything works great except on label/category. Unfortunately i only get one label displayed. How to loop trough each post label from blogger post? The post not only have one label, it have 2 or 3 label.

Here is the code:

var numposts = 5;
function whatshot(json) {
    for (var i = 0; i < numposts; i++) {
        var entry = json.feed.entry[i];
        var posttitle = entry.title.$t;
        var category = entry.category[0].term;
        var summary = entry.summary.$t;
        var category;
        var posturl;
        var thumb = entry.media$thumbnail.url;
        var thumb = thumb.replace("s72-c", "s1600");
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }           
        }               
        document.write('<ul class="collection top-highlight"><li class="collection-item avatar"><img class="circle" src="'+thumb+'"/><span class="title">Lipsum</span><p>'+category+'</p></li><li><p class="sp-status">'+summary+'</p></li><li><img class="responsive-img" src="'+thumb+'"/></li><li><h2 class="sp-title"><a href="'+posturl+'">'+posttitle+'</a></h2></li></ul>');
    }
}
1

There are 1 answers

2
AudioBubble On BEST ANSWER

Use for to loop through category items and get all post labels

for (var m = 0; m < entry.category.length; m++) {
    category += entry.category[m].term + " ";
}

Note space after each label name

Here is your code

var numposts = 5;
function whatshot(json) {
    for (var i = 0; i < numposts; i++) {
        var entry = json.feed.entry[i];
        var posttitle = entry.title.$t;
        var summary = entry.summary.$t;
        var category;
        for (var m = 0; m < entry.category.length; m++) {
            category += entry.category[m].term + " ";
        }
        var posturl;
        var thumb = entry.media$thumbnail.url;
        var thumb = thumb.replace("s72-c", "s1600");
        if (i == json.feed.entry.length) break;
        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                posturl = entry.link[k].href;
                break;
            }           
        }               
        document.write('<ul class="collection top-highlight"><li class="collection-item avatar"><img class="circle" src="'+thumb+'"/><span class="title">Lipsum</span><p>'+category+'</p></li><li><p class="sp-status">'+summary+'</p></li><li><img class="responsive-img" src="'+thumb+'"/></li><li><h2 class="sp-title"><a href="'+posturl+'">'+posttitle+'</a></h2></li></ul>');
    }
}