Tammy Hart Designs

How to Display the Parent Page Title in Your Title Meta Tag

In a project I’m working on at the moment, The page structure is set up using 5 main pages, and then each have their respective sub-pages. For SEO purposes, I wanted my <title> meta tag to display the page title and it’s parent page title if the current page was a child page, i.e.:

<title>Page Title</title> 

or 

<title>Parent Page Title — Page Title</title>

So after doing a little searching and combining some code, this is what I came up with:

<title><?php
if($post->post_parent) {
	$parent_title = get_the_title($post->post_parent);
	echo $parent_title.' &#8212; ';
} ?>
<?php wp_title(''); ?>
</title>

27 Responses to “How to Display the Parent Page Title in Your Title Meta Tag”

  1. Web Designer Kent  

    Ive been working on CSS based sites for sometime but still get the odd error in firefox ! wish they just played ball the same when designing, would make life a whole lot easier for everyone involved… but then that would be to simple and obvious…lol

  2. Title Posting With Title Parent « CP - Contek Pakai  

    [...] mempunyai judul satu halaman dengan menyertakan halaman induknya. La aku nyoba googling dan menu di sini dan secara singkat aku Copy Paste code yang ada di bawah ini file page.php. <?php [...]

  3. Evan  

    Thanks! This helped.

  4. Josef  

    thanks! been looking for a way to display the page parent title.

  5. neel  

    Thanks for this cool tip.. it helps me when i create tabs of subpages from a parent page.

  6. Ion  

    Thanks! exactly what was looking for

  7. Adam  

    Magic, thankyou :)

  8. niki  

    where do I put the code? I’m also a little confused with each post I do. Am I going to have to change this code everytime I make a new child page.

    • Tammy Hart  

      This should go in the header.php template between the tags.

  9. Paris Vega  

    Tammy to the rescue, again! This also works nice if you need to display the title on the page itself between heading tags.By the way this post is #1 in Google for “wordpress display name of parent page”

  10. Graham  

    Thanks for the clear explanation Tammy – most useful. Does this work inside a WP_Query setup do you know?

  11. Guitar Teacher Kent  

    can someone help me please im having probems inserting code also, this doesnt make sense at all

  12. Tammy Hart  

    Kent,

    Can you tell me which part you’re having trouble with and hopefully I can help you.

    Graham,
    Yes, $post->post_parent will work within a WP_query.

  13. kasia  

    Hi there,

    and is there any way to make the parent clickable? also, is it possible to limit parents to one only?

    Thanks :)
    Kat

  14. Tammy Hart  

    kasia,

    This code is meant to be in the of your document, so you can’t place a link there. Maybe the code here can point you in the right direction: http://www.tammyhartdesigns.com/tutorials/wordpress-how-to-list-child-pages-in-sidebar/

  15. Fafnishart  

    I’m about to go nuts in here…I can’t get it to work properly. I am still a PHP rookie and need some advice…Please help me Tammy.
    I have no “header.php” within my theme files and simply have no idea what file to use amongst my “layouts” directory…
    I’d greatly appreciate your help. Thanks in advance!

  16. Tammy Hart  

    Fafnishart,
    Sounds like you have an overly complicated theme. I really wouldn’t be able to tell you what template to use without looking at the theme files myself. Send a list of the directory, or like a screenshot that shows all the files to tammy AT this-domain.

  17. Amp  

    Great code that works fine.
    Thanks. But what if you have a static homepage? Could you use the is_front_page statement? I’ve tried it with WP 3.1 but with no luck. Then again, I’m not too great with PHP.

    Thanks again!

  18. Tammy Hart  

    Amp,

    Not sure what you’re referring to here. This code looks to see if the current page is a child of another page and if that’s true, it adds the parent’s page title to the title tag.

  19. Amp  

    What I mean is this;
    my generic title tag content:
    » Archive

    If I replace that code with your code then the post title of the static home page is missing. What you get is the URI of the blog as meta title.

    Does this make any sense?

    BTW, thanks for your quick response.

  20. Amp  

    Sorry, forgot the code tag.

    Generic title content:

    » Archive

  21. Amp  

    Guess that’s not supported.

    Anyways, it’s plain WP theme title content.

  22. Tammy Hart  

    Amp, this code isn’t meant to replace your title tag code entirely, it’s only an example. I suggest using it in addition to several if else statements for different areas of your site. For instance, my title tag on my site is 17 lines of code.

  23. Amp  

    I understand. I suppose I was trying to get you to give me some nice code for free. ;-)

    Thanks again for the code and for your reply.

  24. Manny  

    Hi Tammy, excellent tip.. tammy i need help, i need change a code but i am not expert i need change this code

    $postheader .= ” . get_comments_number() . ”;

    This function displays the number of comments of the post, sooo i need to change this funsion for showing the category of where the post ..I hope you can help me, thanks in advanced.

  25. Chris Wiegman  

    Thank you! I am working on a simple enough site, but they wanted a breadcrumb shown and this is exactly what I needed for the template file :)

Leave a Reply