Join Morten Rand-Hendriksen for an in-depth discussion in this video Make functionality resilient with PHP, part of WordPress: REST API.
In the exercise files for this movie, 04_02, you'll find that the twentyseventeen-child theme has been expanded a little bit. In addition to the style and functions file, I've added a third file, called single.php, and this is a modified version of single.php from the twentyseventeen theme. I'll show you the difference. So here we have the parent theme single.php. This is a standard loop file for a single.php. It starts with a call to the header. Then we set up the HTML framework for the posts. Here we have a div wrap, then we have a div primary, and a main main, and then we have the post loop in here.
The post loop ends, then we call in the sidebar, and then we get the footer. The single.php file and the child theme is a little bit different. Here I first called the header like before, then I pulled the post loop out to appear above the rest of the content. This is so that we can refer to the post object at any time, not just inside the main. Then we set up the HTML structure as normal, get the template part for the post. This is the actual post itself. And then we have a new section here, nav class="navigation post-navigation load-previous".
This section is what adds the button you see here in my browser. And this is straightforward HTML and regular WordPress code but there is one significant difference between the markup here and what you normally see in a WordPress theme. If we look closely at this nav-previous link, you'll see in addition to pointing at the permalink, which is pointing at the link to the previous post, I've also added an additional attribute here called data-id, and this data-id attribute contains the ID to the previous post.
- Using a REST client to communicate with a REST API
- Reviewing available routes, endpoints, and arguments
- Adding custom post types and taxonomies
- Altering the API response
- Using PHP for resiliency
- Passing info using wp_localize_script()
- Setting up a REST API request through AJAX
- Outputting selected post data to the front end
- Formatting post data to match a theme
- Triggering AJAX for an infinite scroll effect
- Using AJAX to load subsequent responses