Support » Fixing WordPress » Link to parent Page?

  • 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

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter kjetilgf

    (@kjetilgf)

    Not possible?
    Wrong forum?
    Thanks :o)

    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.

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

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

    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;
    ?>

    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.

    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 🙂

    Flash is the man.

    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 } ?>

    Nice!

    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

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Link to parent Page?’ is closed to new replies.