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!

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

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required