How to fetch and display parent categories in post?

Hey! I would like to include the categories and their parents in my blog posts. Is this somehow possible to fetch? Unfortunately, state.source.category(categoryId) doesnt seem to have the parent ids, only links in _links.up element.


I think the problem you are having is due to the fact that when you access one post, the category data is populated with the _embedded part of that post. What you could do is doing another fetch to the specific endpoint of the categories and that way the state.source.category[id] will have all the info.
Actually, we discovered this bug because another user was doing something similar.

By the way, you can access the categories of a post at[id].categories. However, if you add a child category to a post, WordPress doesn’t add the parent categories automatically, so they are not shown at that endpoint.

In order to solve that, you have to make sure to check the parent category while defining the post:

Parent category

Another option would be to modify the REST API endpoint (or create a new one that solves this) where you add the parent categories to that field too.

Hope you find this useful, let us know if it works in your case :slightly_smiling_face:

Thanks for the information! However, this is not really what I want, sorry for not explaining it in detail. My use case is that I would like to show breadcrumbs for SEO and a better navigation.
So I think I’ll expose the “category tree” via REST API using custom fields - I’ll post my solution including the building process of the custom fields as soon as I’m finished with that :slight_smile:


That’s great! We’d love to take a look at that solution, I’m pretty sure it could be useful for other users too :slightly_smiling_face: Thanks!