10

I would like to ask that, how to get the link back to parent page when i'm in the child pages? I would rather to use a proper link instead of <a href="javascript:history.back()">Go Back</a> method.

Question & Example: How can i get the link of 2nd level child page when i'm in 3rd level child page?

Home > Parent Page > 1st Level Child Page > 2nd Level Child Page > 3rd Level Child Page

Home > Parent Page > 1st Level Child Page(this page link show up) > Current Page

Home > Parent Page(this page link show up) > Current Page

Updated line: I have added these three examples above for better explain in what i'm trying to ask.

This question is not about breadcrumbs. I would just like to get the link of parent page to show up when it's in child pages.

And how can i get the link of 1st level child page when i'm in 2nd level child page?

The reason i am asking this question is that i want to create a Back To Previous Page link for different level when i'm in different child page. Please tell me if you don't understand.

18

You can use something like this to get the parent page URL (and show its page title):

<?php if ( $post->post_parent ) { ?>
 <a href="<?php echo get_permalink( $post->post_parent ); ?>" >
    <?php echo get_the_title( $post->post_parent ); ?>
 </a>
<?php } ?>

If you are running this code outside of the loop (thanks @BorisKuzmanov), then use this:

<?php global $post;
  if ( $post->post_parent ) { ?>
    <a href="<?php echo get_permalink( $post->post_parent ); ?>" >
    <?php echo get_the_title( $post->post_parent ); ?>
    </a>
<?php } ?>
| improve this answer | |
  • 1
    it doesn't work. – Jornes Jul 2 '15 at 2:01
  • What do you mean, it doesn't work? Where are you putting this code? – Monkey Puzzle Jul 3 '15 at 0:33
  • 1
    If you are running the code outside a loop, try adding global $post at the beginning of your code. – Boris Kuzmanov Jun 8 '16 at 12:08
  • 1
    If I use either snippet of code on page.php (for example), they both work, both outside of a loop ...should they not? Or does it depend on your setup/theme? And thanks by the way, this is great! :) – user1406440 Apr 19 '17 at 17:41
  • 1
    Working , confirm working, and helped me bring the ID of the parent, to make some conditionals I needed to do!! – Rodrigo Zuluaga Jun 11 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.