JSFiddle - React, Tailwind, and code Playground
HTML
<div class="facebookfeed">Loading...</div>
JavaScript
function fbFetch() {
var access_token = "!!!CHANGE_ME!!!";
var url = "https://graph.facebook.com/?ids=footengo31,footengo01,Footengo69&fields=posts.limit(5){message,created_time,picture}&access_token=" + access_token;
$.getJSON(url,function(response) {
var messages = [];
Object.getOwnPropertyNames(response).forEach(function(page, idx, array) {
response[page].posts.data.forEach(function(post, idx, array) {
messages.push(post);
});
});
function compare(a,b) {
if (a.created_time < b.created_time)
return -1;
if (a.created_time > b.created_time)
return 1;
return 0;
}
//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
var html = "<ul>";
//loop through and within data array's retrieve the message variable.
$.each(messages.sort(compare), function(i,fb){
if (typeof fb.picture != "undefined") {
html += "<li>" + fb.message + "</br>" + '<img SRC="' + fb.picture + '">' + "</br>" + fb.created_time + "</li></br>";
}
else{
html += "<li>" + fb.message + "</br>" + fb.created_time + "</li></br>";
}
});
html += "</ul>";
//A little animation once fetched
$('.facebookfeed').animate({opacity:0}, 500, function(){
$('.facebookfeed').html(html);
});
$('.facebookfeed').animate({opacity:1}, 500);
});
}
fbFetch();