/*
This snippet requires the WordPress JSON feed plugin
Found here: http://wordpress.org/extend/plugins/json-feed/
The plugin provides a new WordPress feed option
Also see my ColdFusion Proxy to avoid Cross-Site Scripting Pastebin:
http://pastebin.com/XTBmUHP0
*/
$(document).ready(function () {
// Cache the "result" div
var blogContent = $('#blogcontent');
// Get the feed via AJAX, in this case, using jQuery 1.7.1
$.ajax({
type: 'get',
// url must be on the same domain as the script
url: 'http://path.to/feed/?feed=json',
dataType: 'json',
beforeSend: function() {
// show an animated gif to indicate something happening
blogContent.html('<p><img src="/i/img/loading.gif" width="16" height="16" alt="Loading …" /> Loading …</p>');
},
error: function(data, status, error) {
// ajax request failed, display an error
blogContent.html('<p>Blog Feed is not available.</p>');
console.log(data); // remove for production
console.log(status); // remove for production
console.log(error); // remove for production
},
success: function(data) {
// clear animated gif
blogContent.html('');
var blogItems = []; // create empty array
$.each(data, function(i, item) {
// only return the first 6 items from the feed
//change to the number you want to display;
if (i === 6) return false;
// add the current item to the blogItems array
blogItems.push('<p>\r\n\t<a href="' + item.permalink + '">' + item.title +'</a>\r\n</p>');
});
// add array of items to the "result" div
blogContent.append(blogItems.join(''));
console.log(data); // remove for production
}
});
});