Function to Find ID of Top-Most Page

I used this recently coupled with breadcrumbs to prevent certain items from showing on the top-most items in page hierarchy.

function get_top_parent_page_id() {
     global $post;
     // Check if page is a child page (any level)

     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;


To check for the top-most page, I added the following condition to my the body:

<?php if(get_the_ID() == get_top_parent_page_id()) { echo 'topmost-page'; } ?>


Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.