Try:
<?php
if( get_the_title($post->post_parent); ) {
$parent_title = get_the_title($post->post_parent);
echo $parent_title;
the_title();
}
?>
alternatively, try:
<?php if( $post->post_parent ) echo get_the_title( $post->post_parent ); ?>
Thanks Esmi and Alchymyth, but aren’t both of the codes you provided what I already pasted above in my question? I know how to show the parent page title if on a parent or child page. The problem I am trying to solve is how to show the child page title ONLY when on a child page. Right now I am using the above code on my page, along with a code that shows the title of the page it’s on. The problem is that when I am on the parent page, it shows the title twice. Once because the page is a parent and once because it is the title of that page.
Does that make sense?
Basically I want code that shows the parent page title when on a child of that parent or on that parent’s page (the above code we all shared that I’ve already got working), but if I’m on one of the child pages, I also want its title to show under the parent’s title.
Thanks so much for the help. Hope my posts weren’t confusing! I’ve been searching for 3 days now trying to find a solution to this. Seems like it should be a pretty standard feature 🙂
Did you try either suggestion? Did you note that both use conditionals to first test to see if you are on a child or parent page?
I did try both. The first one showed first the parent, then the child when on a child page, but showed the parent title twice when on a parent page 🙁 The second example doesn’t work either. Basically, I need two separate codes that say:
if on parent or child, show parent title here as h1
and
if on child, show child title as h2, but if on parent, show nothing