Skip to content

Instantly share code, notes, and snippets.

@ademuk
Created October 5, 2012 13:32
Show Gist options
  • Save ademuk/3839834 to your computer and use it in GitHub Desktop.
Save ademuk/3839834 to your computer and use it in GitHub Desktop.
/* Library Code */
(function(global, $) {
function Wordpress(url) {
this.init(url);
}
Wordpress.prototype = {
init: function(url) {
this.endpoint = url;
},
getCategories: function(cb) {
$.getJSON(this.endpoint + '?cb=?&json=get_category_index', function(response) {
cb.call(global, response);
});
},
getPosts: function(cb, slug, count) {
var count = count || 10,
url = slug ? 'json=get_category_posts&slug=' + slug + '&count=' + count : 'json=get_recent_posts&count=' + count;
$.getJSON(this.endpoint + '?cb=?&' + url, function(response) {
cb.call(global, response);
});
}
}
global.Wordpress = Wordpress;
})(this, jQuery);
/* Example Use */
var wordpress = new Wordpress('http://wordpress_url');
// Get categories
wordpress.getCategories(function(response) {
$.each(response.categories, function(i, el) {
//TODO draw HTML
})
})
//Get latest posts
wordpress.getPosts(function(response) {
$.each(response.posts, function(i, el) {
//TODO draw HTML
});
});
//Get posts in category-1
wordpress.getPosts(function(response) {
$.each(response.posts, function(i, el) {
//TODO draw HTML
});
}, 'category-1');
//Get top 20 latest posts
wordpress.getPosts(function(response) {
$.each(response.posts, function(i, el) {
//TODO draw HTML
});
}, null, 20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment