Created
February 25, 2014 18:04
-
-
Save scottopolis/9214367 to your computer and use it in GitHub Desktop.
Ajax WordPress posts via JSON API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button class="button" id="get_posts">Get Posts</button> | |
<ul id="statuses"> | |
</ul> | |
<script type="text/javascript"> | |
/* Requires JSON API plugin installed on remote site http://wordpress.org/plugins/json-api/ */ | |
(function(window, document, $, undefined){ | |
'use strict'; | |
var app = {}; | |
var $body = $('body'); // cache body element | |
var rootURL = 'http://yoursite.com/'; // change to your site url | |
var getPostsURL = rootURL + 'api/get_recent_posts/'; | |
app.initialize = function() { | |
$body.on('click', '#get_posts', app.ajaxRecentPosts ); | |
} | |
app.ajaxRecentPosts = function() { | |
// Get recent posts | |
$.ajax({ | |
url: getPostsURL, | |
dataType: 'jsonp', | |
cache: false, | |
success: app.getPostsLoop | |
}); | |
console.log('ajaxed posts'); | |
} | |
app.getPostsLoop = function(data){ | |
// Empty so nothing is repeated | |
$('#statuses').empty(); | |
// Loop through post data, display title and content | |
$.each(data.posts, function(index, value) { | |
$('#statuses').append('<li class="item">'+value.title+value.content+'</li>'); | |
}); | |
} | |
app.initialize(); | |
})(window, document, jQuery); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment