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

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

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

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

  6. 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?

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

  8. 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?

Comments are closed.