Get parent page url to show up when it is in child pages

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()" rel="nofollow noreferrer noopener">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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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 ); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" >
    <?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 ); ?>" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" >
    <?php echo get_the_title( $post->post_parent ); ?>
    </a>
<?php } ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments