How to get top parent page ID in wordpress?

How to get top parent page ID in wordpress

The solution to get the top parent page id is here, it is quite easy.


function wp_get_top_parent_page_id() {

    global $post;

    // Check if page is a child page
    if ($post->ancestors) {

    //  Grab the ID of top-level page from the tree
    return end($post->ancestors);

    } else {

    // Page is the top level, so use  it’s own id
    //return $post->ID;
    return 1;

    }

    }

echo wp_get_top_parent_page_id();

Comments