On a recent project, my client had several microsites where he wanted to display the same blog posts from one central site. So I decided to simply use the WordPress REST API to do this.
Accessing the WordPress REST API
I’m not going to go into details of what the WP REST API is but to access it simply add
/wp-json/wp/v2/posts to the end of any WordPress site URL. This will give you a list of posts in JSON format.
So this is the code I used: [gist https://gist.github.com/rickalday/61a1e6fcefa13547de4c94d5059893f8 /]
I appended two parameters to the API URL:
embedded parameter allows me to access the post featured image amongst other things.
posts_per_page is simply the number of posts I want to be returned.
There are other arguments you can append to get specific posts.
Feel free to play with the code.