How To Display Parent Page Title in Wordpress

Display Parent Page Title

Have you ever tried to show the title of a grandparent page, while you are on a subpage? Your search has ended because I am willing to share this information with you.

This is great for those who want to have a sidebar navigation with grandchildren pages (1+ subpages) and want to have all the time the grandparent page show up.


Here is the working example, on a website I helped developing. You can see how the parent title on the left sidebar, remains unchanged and is dynamically updated, for all of the other pages.

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

<?php 
 
$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.

 
<?php
 
$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;?>

Liked this page? Share:

  • Twitter
  • Facebook
  • LinkedIn
  • Digg
  • StumbleUpon
  • del.icio.us

5 Responses to “How To Display Parent Page Title in Wordpress”

  1. Being brand new at Wordpress, I cannot seem to get my head around the parent, child, grandchild this and have managed to mess up the Sidebar Category Listings with posts appearing everywhere!

    Then I came across your suggestion on the forum which is how I arrived here.

    Where exactly would I put the code you suggest above? On which page in the directory? And where on that page. Does it replace anything or is it a stand alone addition?

    To recap, I would like to see in the sidebar:

    Category Subject 1
    – Subtitle of Category Subject
    – Post 1 of this subtitle
    – Post 2 of this subtitle

    etc

    If you could spare the time, I’d appreciate your suggestions/advice.

    Would you prefer to do this within the Forum? If so, let me know and I will post there.

    Yours
    Chris McCready

  2. The code needs to stay within the wordpress loop.
    Please note, the code is meant just for the title of the parent/grandparent page, and will not replace the menu itself.

  3. Great tip! thanks a ton :)

  4. Thanks, this was just what I needed. It’s also possible to echo get_the_title(…) and save a line of code.

  5. Thanks, this was very helpful just now.

Leave a Comment