Post Pic

How to: Show parent page title regardless of what subpage you are on

Althougt it may sound simple to advanced WordPress coders, I had many requests from people working with WordPress as a CMS and wanting to be easily able to display parent page title on a subpage.

Nothing hard at all: Just paste the following code where you'd like to display the parent page title:

<?php
if($post->post_parent) {
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
} else {
    wp_title(''); 
}
?>

That's all! Quick and easy, as we like :)

This recipe has been submitted by Wes Bos. Have WordPress skills? Feel free to contribute to WpRecipes!

7 Responses

Oct 24 2011 12:15

Great!! Thanks alot!
Is there a way to make a link too? (link to itself – the parent)

Jan 10 2012 18:16

Thanks for the tip! Quick question: Any idea how would you write the function if you wanted to show the root parent page on a grandchild page? I’ve been tinkering, but no cigar (yet).

Jan 19 2012 16:14

Thank you!
Can you explain how can I display not the parent’s titile, but parent’s menu label?

I’m using All In One Seo pack, where I use special field for menu — its much shorter comparing to long titles.

Feb 01 2012 10:18

Please a little help here: this one is bugging me since long time.

I would like want to display the title of the parent page only if there is one. All the recipes I’ve found so far (including this one) will show the name of the current page when it is a root-level page. This ends up in a double title.

May 29 2012 08:53

Did you got a way to link to a parent menu ? So I can use this idea as my breadcrumb. Please advice. Thank you.

May 29 2012 08:56

Hey I got the way :-)
$parent_title = get_the_title($post->post_parent);
$parent_link = get_permalink($post->post_parent);
$parent_title

Hope this will help some one. Thank you..

Jul 27 2012 23:57

Does this work with grandchildren as well?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox