Forums

Link to parent Page? (12 posts)

  1. kjetilgf
    Member
    Posted 4 years ago #

    Tried to search, but no luck:
    How can I insert a link in a sub-page that points to its parent page?

    Such a short php snippet for my main template would make it much easier building the rest of my site. If someone can help me, please include the brackets and all - I'm not much of a handcoder.
    Thanks,
    Kjetil

  2. kjetilgf
    Member
    Posted 4 years ago #

    Not possible?
    Wrong forum?
    Thanks :o)

  3. flashaaron
    Member
    Posted 3 years ago #

    A bit late, I know, but you could try adding something along the lines of:

    <?php if($post->post_parent) {
    	$parent_link = get_permalink($post->post_parent); ?>
    <a href="<?php echo $parent_link; ?>">Link to parent page</a>
    <?php } ?>

    to your template file.

  4. mashby
    Member
    Posted 3 years ago #

    That was just the solution I was looking for. Thank you flashaaron!

  5. OneEyedParrot
    Member
    Posted 3 years ago #

    Thanks flash! You did a good thing to post the solution in a google-able topic. :)

  6. particulartastes
    Member
    Posted 3 years ago #

    So extremely useful flashaaron!

    Use in combination with information from this page:

    http://wordpress.org/support/topic/160732?replies=5

    Replace "Link to parent page" in the above code with the code below to specifically call whatever the parent's name is:

    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>
  7. tanchiahuei
    Member
    Posted 3 years ago #

    Thanks flashaaron!

    I was creating all the other sub-page navigation links manually in a list since i'm not great with wordpress loops yet. Then I ran into problems linking back to the parent page, because it would have double the permalink e.g. creatine_guide/creatine_guide :p

    Thanks very much for the bit of wordpress code, saved me hours of searching.

  8. tanchiahuei
    Member
    Posted 3 years ago #

    For the benefit of other users.... I stumbled upon a small problem with the earlier code. On the child pages, the parent page link appears. However, on the parent page itself, it's not there!

    I finally worked it out by repeating the echo parent link part:

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

    At least it worked for my system, where I only had 1 parent page. Hope this is useful for others :)

  9. misternifty
    Member
    Posted 3 years ago #

    Flash is the man.

  10. misternifty
    Member
    Posted 3 years ago #

    I just added to this code so it grabs the parent page's title and dynamically changes the link per page.

    <?php if($post->post_parent) {
         $parent = $wpdb->get_row("SELECT post_title FROM $wpdb->posts WHERE ID = $post->post_parent");
         $parent_link = get_permalink($post->post_parent); ?>
         <a href="<?php echo $parent_link; ?>">Back to <?php echo $parent->post_title; ?></a>
    <?php } ?>
  11. elC0mmen
    Member
    Posted 3 years ago #

    Nice!

  12. tison
    Member
    Posted 3 years ago #

    This may be off topic, but if you're hoping to use this display a breadcrumb type list - this post is very helpful in doing so:

    http://wordpress.org/support/topic/179226?replies=13

    Also, here is some available codes for $post:

    http://wordpress.org/support/topic/168285

Topic Closed

This topic has been closed to new replies.

About this Topic