WordPress Posts in React

Latest Post Status Posts

In an attempt to show as simple of a React app as possible, I am stealing Chris Coyier's example that shows CodePen jobs. I wanted to show an example using WordPress post data.

If you fork this Pen, you can swap out the URL from Post Status to your own website, if it's running v2 of the WordPress REST API .

The author and featured image data is made available thanks to the ?_embed query string, which adds additional information to the return data: author, media, terms, and comments are all included, for example.

The query for the following posts looks like this:

https://poststatus.com/wp-json/wp/v2/posts/?_embed&per_page=3&author=1

To see something similar with Vue.js, check out this Pen .

Notes

For the featured image, if you don't want to reference a specific size, you could call post._embedded['wp:featuredmedia'][0].source_url instead of post._embedded['wp:featuredmedia'][0].media_details.sizes["large"].source_url and the full size image would be returned. You can also replace the large size with other image sizes. I used the syntax shown in case your image size has a hyphen in it.

Additionally, you can change the post type to something like pages or a custom post type to see more results.

To change other things, like per_page and author , just add or adjust the appropriate query parameters.

There are a few potential "gotchas" if you run into an error. I don't have a lot of fallback logic in case the right data isn't available. If you have trouble, that's the most likely source of the issue.