As the title suggests, I am trying to find out if a certain page has another page as a parent or grand parent or great grand parent etc…
To check for the parent case, the following works
if (1 == $post->post_parent) {
but when checking for the ancestor
if (1 == $post->post_ancestor) {
does not seem to work.
Any ideas?
Thanks
There’s no built in support for this, but you can use this helper function, just put it in your functions.php:
It will return the ID of the post that is the ancestor, so you could use it like this:
In addition to the function provided by “ninja”, also note that your condition is not correct: the “post_parent” contains the ID of the parent page, or 0 if the current page is a parent page.
So the actual condition to ask if a page has a parent is: