Have you ever wanted to automagically load new post to the bottom of your front page like Twitter do (you might want to look at WP More Posts)? Maybe you want to load some pages with AJAX and display them in a fancy lightbox like I do (try to click on about or contact!)? What if I say it’s all possible in WordPress and it’s really simple?
What WP JSON Posts does is returning any page, post or feed you like as JSON. If you want to load a page into a lightbox you shouldn’t need to use something else than the actual URL. And that’s exactly how easy WP JSON Posts is to use. An AJAX request to
http://example.com/this-is-my-page?jsonposts=1 will give you a JSON representation of that page.
I’m sure you already got some ideas about how you can implement this in your own theme or plugin. Go ahead and download WP JSON Posts now! Otherwise just read on and I’ll show you just how deep the rabbit… how much you can accomplish with this WordPress plugin.
- Get JSON data for any post, page, archive, the front page etc.
- It works even for custom posts and taxonomy archives.
- And all this are enabled completely automagically.
- Choose what data you need.
- Customize number of posts and what posts you’ve already got.
- No bloat. No unnecessary functions. You are free to do whatever you want.
- It’s free! Licensed under GPLv3.
Note: custom queries, page templates and other stuff you put in the template files are ignored. That’s kinda the definition of this plugin.
What exactly does it do?
The strength of WP JSON Posts is that it’s based on WordPress own built in post query. That is, when you visit a certain URL WordPress does some hard work figuring out what you want to see and goes to get those posts. Usually WordPress then applies the correct template, be it single.php or taxonomy-whatever.php. But if WP JSON Posts finds a key named ‘jsonposts’, the theme is swapped for JSON.
As of version 1.0.1 the following data can be returned for each post:
- The content already HTML-formatted
- An array for every category as an object. The same data as in the WordPress category-object plus the url.
- Date formatted as “2009-06-11 10:58″. May be subject to change in future updates.
- Array of image properties:
- crop (bool)
There’s one more thing…
Well, four actually. These are the parameters you can send as GET requests to customize what post data that are returned. In summary you could send
The above data contains pretty much a shitload of data which might be unnecessary. Often you only want the title and the featured image. Thats where the &exclude parameter comes in handy. My plugin expects it to be a dash-separated string of properties to remove. For example
&exclude=the_content-the_date-the_category would remove those properties from the returned post objects. Default is to send everything.
Sometimes you don’t want to get the full normal result of a feed, but only the first couple of posts. For example on the front page of my portfolio I want to get a different number of posts depending on how long the rows are.
&number_of_posts=5 will take care of this.
The third parameter you may attach is &offset. This is used to specify how many post you’ve already got, just like WordPress’ own offset parameter.
&offset=10 would start getting posts at the eleventh post. Default is 0.
Finally you might have to specify what size you want the featured image to be.
&imgsize=case would return the image size named “case”. Default is “small”.
By now I’m really sure you’ve got a few ideas how to use this plugin. Why don’t you download WP JSON Posts now? And don’t hesitate to write some feedback in the comment section below.