In WordPress, pages can be hierarchical. So, you can set a page as a parent or child of another page. If you want to be able to show the title of the current page’s parent, here’s a quick PHP code snippet you can use.
This snippet will help you detect if the current page has a parent and if it does, then it will output the title of the parent page:
// 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 ){
$title = get_the_title($post->post_parent); // Get the title of the parent page.
echo $title;
}
The above code first gets the current $post variable. This variable contains the current page that the user is on. Then it checks if the current post has a post_parent. If it does, then it gets the title of the post parent using get_the_title and outputs it.
You can also get the ID of the parent page the same way:
global $post; // Get the current post (page).
// Check if this page has a parent page.
if ( $post->post_parent ) {
echo $post->post_parent; // Output the ID of the parent page.
}