• I’ve been using this beauty of a code snippet for when I want to show the page’s title if it’s a parent page and show the parent title when on a child page:

    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>

    However, ONLY when on a child page of that parent, I would like the child page’s title to show underneath.

    So, if the page parent was “About Us” and its child page was “History”

    I’d like the page to show below when on the About Us page:

    About Us

    And this when on the History page:

    About Us
    History

    How do I do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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 ); ?>

    Thread Starter isign4jc

    (@isign4jc)

    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?

    Thread Starter isign4jc

    (@isign4jc)

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to show child page title only when on child page’ is closed to new replies.