Couple days ago Ryan asked me can we use
slug instead of
ID, and reminded me that in the original demo video (Eric Wolfe: Building a WordPress Theme Using AngularJS), Eric did use the
slug rather than
ID. At first I replied to him it’s because Eric used the JSON API not WP API (JSON REST API), by default the WP API can only get post by ID.
So here are the snippets you’ll need to update, if you prefer to use slug in our project files (from my previous tutorials):
- line 6: Change
- line 16: Change
'wp-json/posts/' + $routeParams.IDto
'wp-json/posts/?filter[name]=' + $routeParams.slug
- line 17: With this approach, we’ll get an array of posts, so we set
$scope.post = res.
- line 5: Change
You can get the full code from a branch of the project repo. I hope short posts like this can also help readers understand better about AngularJS and WordPress, especially how to accomplish a specific task. See you soon.