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!

5 Responses

May 11 2012 18:12

Pretty awesome thanks for the help

Cheers :)

May 24 2012 12:51

Thank you!

I had some code that worked but when there were parent pages with two words it place a hyphen between them. This is much cleaner and no hyphen! ;-)

Jul 24 2012 09:15

Thank you very mach,

very simple and clean code

Nov 15 2012 15:05

Thank you :-)

An if-function would also be helpful

May 06 2013 14:36

Doesnt work if you have more than 2 level.

Like:
Page 1 -> Page A -> Page Level 3 -> Sub Page (level 4).

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox