How To Display Parent Page Title in WordPress

The following code display the page title and show the parent page title if exist

if($post->post_parent) {
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
else {
echo get_the_title($post->ID);

To make it work, just insert the code anywhere in your wordpress template files, within the loop:

$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
<?php if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {echo get_the_title($grandparent); }else {echo get_the_title($parent); }?>

In case you want to display the immediate parent of a subpage, you can use the following code. In this portion of code, you have a link to the parent page itself.

$parent_title = get_the_title($post->post_parent);?>
<a href="<?php echo get_permalink($post->post_parent) ?>"><?php echo $parent_title;?></a>
<?php echo $children;?>