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!

18 Responses

Oct 30 2009 15:16

Often it’s useful to always show the top/grand parent of a page. This snippet only show the current pages parent.

Oct 30 2009 16:17

@Erik Petterson: You can easily extend this snippet to display the top parent title:

post_parent) {
$ancestors = get_post_ancestors($post->ID);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
$parent_title = get_the_title($parent);
echo $parent_title;
} else {
wp_title(”);
}
?>

Based on a snippet found here: http://cssglobe.com/post/5812/wordpress-find-pages-top-level-parent-id

Oct 30 2009 16:27

@Erik
If you want to get the Top most parent you will need a loop in the 1st section of the IF statement.
“$post->post_parent” is the page id of the parent so you would want your loop to look something like the below:
while($post->post_parent){
//Load $parent page
//Echo the Page Title if you want to create a breadcrumb
$post->post_parent = new posts parent_id
}
//Echo out Title of last loaded post. This will be the top most parent

Nov 09 2009 17:39

This was very useful. Appreciate it!

Nov 17 2009 13:34

something I am using, having deeply nested pages is this code:

$ancestors = get_post_ancestors($page_ID);
if (count($ancestors) > 0){
$top_most_page_ID = end($ancestors);
}

Now you have the id of the first ancestor. The $page_ID will probably be $post->ID in most cases, but you can call it with what ever you’d like.

Thanks for you post Wes Bos

Nov 20 2009 09:33

I’m wondering if its possible to show the content (rather than the title) from the parent page on all child pages?

I am using the parent page as a redirect to the first child, but I’d like to be able to set some text which is shown on all children (the text needs to differ from the parent page title and slug).

Nov 23 2009 00:17

I made a little changes to show the post parent and title togheter

if($post->post_parent) {
echo (” . get_the_title($post->post_parent) . ‘ » ‘ . $post->post_title . ”);
} else {
the_title();
}

Jan 07 2010 12:34

That’s really awesome info for me. I was really in need of this and finally got it here.

thanks a lot for your help

Feb 06 2010 17:46

Is there a way to use this code for the title tag in the header?
I want to show the site structure like
“Sup page title – Parents Page title | Blog title”
At the moment on Sub pages I only see
“Sup page title | Blog title”

I’m using the plugin All-in-One SEO to rewrite the title tag, but there I couldn’t find any option to add parents page title.

Thanks for your help!!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required