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.
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
Often it’s useful to always show the top/grand parent of a page. This snippet only show the current pages parent.
@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
@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
This was very useful. Appreciate it!
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
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).
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();
}
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
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: