
If you use pages and subpages or posts and parent posts on your WordPress blog, it should be a good idea to display parent page/post title while on a child page. Here’s a code to do that job easily.

If you use pages and subpages or posts and parent posts on your WordPress blog, it should be a good idea to display parent page/post title while on a child page. Here’s a code to do that job easily.
To achieve this recipe, simply edit your page.php file and paste the following code where you'd like your parent page title to be displayed:
<?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?>
That's all. Also, this code should be some inspiration for creating breadcrumbs. See you tomorrow!
18 Responses
Thx
How can i link the title?
I assume this returns the title of the immediate parent and not the top-level parent (in cases where the current post is the grandchild or beyond)?
Did you read my mind and scan the question I had? Absolutely brilliant.
I am also interested in learning how to link the title to the parent as well as how to get the top-level parent. Better yet, is a breadcrumb type of listing of parent hierarchy possible?
Btw, love your site!
@K: Yes, this should be possible, maybe for a future recipe
Thanks Jean. I’ll look forward to it.
Awesome! Just what I needed! Thx!
because top parent have themself as parent
here the code to get the top parent :
[code]
global $post;
$topParent = $post;
while ($topParent ($topParent->post_parent)) {
$topParent = $topParent->post_parent;
}
echo (get_the_title($tempParent));
[/code]
hey micky2be,
whereabouts do you put this code? It breaks sidebar and doesn’t work when I put it in the functions.php either..
Has anybody figured out, how to display the top level parent? The code provided by micky2be doesn’t work.
global $post;
$topParent = $post;
while ($topParent->post_parent) {
$topParent = $topParent->post_parent;
}
echo (get_the_title($topParent));
yet again a post that helps me… this is my favourate wordpress site… awsome.
Thanks much!
Here’s what I use to ‘go back’ to the parent page.
if (is_page() && $post->post_parent) {
$post_parent = get_post($post->post_parent);
$parent_link = ‘post_parent).’”>’.$post_parent->post_title.’‘;
echo $parent_link;
}
I have figure it out: http://www.fldtrace.com/wordpress/how-to-display-parent-page-title-in-wordpress
Here’s what I recently solved.
It would produce an array called ‘parents’ which contains the data of the parents of a post/page
$parent = $post->post_parent;
while ( $parent ) {
$page = get_page( $parent );
$parents[] = $page;
$parent = $page->post_parent;
}
Thank you so much!
hugs from Brazil
// Top Level Parent
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
echo $parent;
Trackbacks: