I’ve been toying with the WordPress JSON API today and I made a JQuery Mobile application that displays posts from my WordPress blog. The query I do is this one ?json=1&callback=cb and it gets me 10 posts. The only problem is: what if someone wants to see the 11th post? I haven’t found any way to get it.
- I could set a count to
20in my query but it’d only transfer the problem to the 21st post. - Increasing the count every time the user reaches the limit would mean that all already seen posts (the 10 first) are downloaded again, for nothing.
I don’t feel good about any of these solutions. I’ve seen you can search a post by:
- its slug: I don’t know it.
- date: I know it is older than that of the post I am currently displaying, but the query needs the real date and I don’t have it.
- category, author: doesn’t help finding that 11th post.
Here’s my code to give you an idea.
HTML:
<div data-role="header">
<a href="#" data-role="button" data-theme="a" onclick="loadPictureFromId(currentPictureId+1)">Previous</a>
<h1 id="title"></h1>
<a href="#" data-role="button" data-theme="a" onclick="loadPictureFromId(currentPictureId-1)">Next</a>
</div>
JS:
var currentPictureId;
var myData;
function cb(data) {
myData = data;
loadPicture(0);
}
function loadPictureFromId(id) {
if (id<0) {
alert("There are no more pictures!");
}
else
{
if (id>9) {
alert("Feature not supported yet.");
}
else
{
currentPictureId = id;
var temp = $("<div/>").html(myData['posts'][id].title).text(); // HTML entities
$("#title").text(temp);
document.title = temp;
$("#img").attr({'src': myData['posts'][id].attachments[0].url, 'alt': temp, 'title': temp});
$("#facebook").attr({'data-href': myData['posts'][id].url});
}
}
}
</script>
If this is the plugin you are using:
http://wordpress.org/extend/plugins/json-api/
Then the parameter you are looking for is
page. If you set page to 2 in your request it will get posts 11 to 20. E.g.?json=1&page=2&callback=cbThe README details this.