Post Pic

How to: Get parent page/post title

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!

21 Responses

Jan 28 2009 11:58

Thx :-)

How can i link the title?

Jan 28 2009 15:14

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)?

Jan 28 2009 20:01

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!

Jan 29 2009 23:54

@K: Yes, this should be possible, maybe for a future recipe :)

Feb 01 2009 15:59

Thanks Jean. I’ll look forward to it.

Feb 10 2009 14:41

Awesome! Just what I needed! Thx!

Feb 16 2009 14:58

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]

Feb 24 2009 23:07

hey micky2be,
whereabouts do you put this code? It breaks sidebar and doesn’t work when I put it in the functions.php either..

May 27 2009 23:27

Has anybody figured out, how to display the top level parent? The code provided by micky2be doesn’t work.

May 27 2009 23:49

global $post;
$topParent = $post;
while ($topParent->post_parent) {
$topParent = $topParent->post_parent;
}
echo (get_the_title($topParent));

Jun 22 2009 19:52

yet again a post that helps me… this is my favourate wordpress site… awsome.

Jul 22 2009 04:48

Thanks much!

Jul 30 2009 03:26

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;
}

Aug 18 2009 04:28
Sep 22 2009 19:45

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;
}

Nov 05 2009 20:12

Thank you so much!

hugs from Brazil

Feb 12 2010 21:42

// Top Level Parent
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];

echo $parent;

Apr 28 2010 20:23

i have a question for you…. How can i HIDE the parent page link when ON the parent page, (yet still showing child pages) and then show it again when ON a child page? Have not been able to find the answer anywhere. PLEASE email me at justin{a}thedesignworks.net if you have an answer for me. Thanks!

Apr 29 2010 14:03

@justine try

if ($post->ID == $parent) { //hide with css }

Apr 30 2010 13:05

@ Justin

This code only shows the parent page if its not the page it selfe. It also ads a > between the two links if there is a parent page. You can ofc change the > to anything you fancy.

post_parent) {$parent_title = get_the_title($post->post_parent);?><a href="post_parent) ?>”><?php if($parent_title “”){ echo ” > “; } ?><a href="” title=”">

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required