Select Page

WordPress allows pages to have parent pages. A page called About Us might have child pages called Our Team and About Our Founders. If you want to check if a page is a sub-page, there’s an easy way. I will also show you how to get the ID of the parent page if the current page is a sub-page.

global $post; // Get the current post (page).

// Check if this page has a parent page.
if ( $post->post_parent ) {
	echo $post->post_parent; // This is the ID of the parent page.
}

The above code is simple. It first gets the current global $post variable. Pages are simply a custom post type in WordPress. So, basically, they are also posts. Then it checks if the current page has a post_parent. If it does, then it echoes the parent page’s ID.

If you want to get the title of the parent page, you can use this code:

// Get the current post object, which in this case will be the current page.
global $post;

// Check if the current page has a parent page.
if( $post->post_parent ){
	echo get_the_title($post->post_parent); // Echo/Output the title of the parent page.
}