如何检查页面是否是Wordpress中另一个页面的直接或间接子页面?

如何检查页面是否是Wordpress中另一个页面的直接或间接子页面?

问题描述:

I have a page with id 63. That page is having a number of child pages and each of the child pages is again having sub pages. In short page with id 63 is super parent to all the pages.

What I want is, I want to check whether the current page is a child of page with id 63. Either a direct child/child of child up to any level.

I am able to check the whether the page is a child of page 63. But not able to check the same for child of child pages.

I am using $post->post_parent == 63 to check whether the page is a child of page 63.

How can I check the same for all levels of pages ?

我有一个id为63的页面。该页面有许多子页面,每个子页面都是 再次有子页面。 在id为63的短页中,所有页面都是超级父级。 p>

我想要的是,我想检查当前页面是否是id为63的页面的子节点。要么直接 任何级别的孩子的孩子/孩子。 p>

我能够检查该页面是否是第63页的孩子。但是无法检查子页面的孩子是否相同。 p>

我使用 $ post-> post_parent == 63 code>来检查网页是否是第63页的子节点。 p>

如何检查所有级别的页面? p> div>

    $pageId= get_the_ID(); 
    function get_topmost_parent($post_id)
    {
        $parent_id = get_post($post_id)->post_parent;
        if($parent_id == 0)
        {
            return $post_id;
        }
        else
        {
            return get_topmost_parent($parent_id);
        }
    }

You can use get_post_ancestors() like this:

$parents = get_post_ancestors($post);
 foreach($parents as $page_id){
    if($page_id == ID of specific parent page){

    // Do something

    break; //Match found, no need to keep checking
    }
 }