Hierarchical structure of custom post type “Book” (for example).
When we are on Post 2-95
, I want to know:
- Does the post has this post ancestor(
Post 1-31
)? - Does it have child posts(
Post 3-19
,Post 3-10
)?
Then, if it has:
- an ancestor post: retrieve (object) of this post.
- a child posts: retrieve (objects) of these posts.
Given a post represented by a post object
$p
, you can find out if post 31 is the parent via:To figure out the children, something like:
Finally, to determine how many levels deep down the hierarchy you are, you will need to recurse up the hierarchy
$p->parent_post == 0
, and then count how many times you needed to do that.e.g.
Check if current post is in range
We check with the function, if we are…
Stick all the functions in your functions.php file.
Check if we got children in range
We check, if we…
In case nothing was found, we return
false
, so we can make our check easier.Check if we got ancestors
We check, if…
In case nothing meets, we again return
false
.Template
Now we can use it in any template like this: