How To Display Parent Page Title in WordPress

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 when you navigate on other subpages.

Static Parent Page Title

To make the static parent page title work, just insert the code anywhere in your wordpress template files, within the loop:

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

</h1>

19 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 !!

  12. Finaly i founded my other good solution!! thanks man!

  13. Great effort! i exactly needed this.

  14. Hey Lucian,

    Thank you so much for this code!

    I still would have a question:

    If I crawl deep enough into sub pages (depth 4->) the title won’t be the top page title anymore. Any ideas how to keep the top page title no matter how deep you crawl?

  15. Sami, thanks for pointing that out, but I never had to try it that deep. For SEO purposes it is not recommended to go that deep with the website structure and you might reconsider the way you present the content.
    However, I believe the code can be adjusted to show the parent title right for the 4th level as well.

  16. Hi Lucian,
    This is just the kind of code I’ve been looking for!
    Thank you for your help!

  17. Lucian

    You cannot even imagine how hard for me was find that solution, thanks very much for sharing that.

  18. This works fantastic, Thanks!

    One quick question- how would you go about hiding the parent title on the parent page itself? I have it displaying on the child and grandchild pages, but unfortunately I now have a double title on the parent page. Any thoughts?

  19. Thanks,

    Obvious solution I missed out.