How To Display Parent Page Title in WordPress

parent-title-thumb

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.

Static Grandparent Page Title

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); }?>

Immediate Parent Page Title

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

11 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.

  6. Hi Lucian,

    Exactly what I was looking for although for some reason I can’t get it to look exactly like yours! I used your code in my sidebar inside a widget:

    ID;
    $parent = $post->post_parent;
    $grandparent_get = get_post($parent);
    $grandparent = $grandparent_get->post_parent;
    ?>

    post_parent);?>
    <a href="post_parent) ?>”>

    The result is the Parent Page and Child Page but no grandchild page.

    Thank you for your help!
    Rebecca

  7. Rebecca, not sure exactly how you use the code inside of the widget, but if you use it normally, inside a template file, it should work. It is the same code I use in two different websites and other people have no problems with it either.

  8. Thanks for ending my search, it works great for me!

  9. Hi, I need to create a two column display with the parent page content (ex. [Company] title and content) on the left and the child page content (ex. [About Us] title and content) on the right.

    How would you go about that?

    Thanks in advance.

  10. I believe we are talking here about multiple WordPress loops which will be more advanced work.

  11. Thank you so much for this !!

Leave a Comment