WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Linking Parent link to page. (3 posts)

  1. arrowoods
    Member
    Posted 4 years ago #

    I am using the below code in my nav. It works perfectly at showing me the top level parent of the page no matter what subpage I am on. The only issue I am having is making it a link back to that page. Can someone tell me what to do to make it link back... I am a php newbie but I am a master cut and paster. Thanks

    <div id="sideNavParent">
    	<?php
    if($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $root = count($ancestors)-1;
    $parent = $ancestors[$root];
    $parent_title = get_the_title($parent);
    echo $parent_title;
    } else {
    wp_title('');
    }
    ?>
    </div>
  2. arrowoods
    Member
    Posted 4 years ago #

    I found the solution so if anyone needs it here it is.

    <?php
    if($post->post_parent) {
    $ancestors = get_post_ancestors($post->ID);
    $root = count($ancestors)-1;
    $parent = $ancestors[$root];
    $parent_title = get_the_title($parent);?>
    <a href="<?php echo get_permalink($parent) ?>"><?php echo $parent_title;?></a>
    <?php
    } else {
    wp_title('');
    }
    ?>
  3. dorianj
    Member
    Posted 3 years ago #

    I'm trying to do this same thing, but only want it to get the immediate parent, not the very top parent. Unfortunately I'm not a master PHP'er, but I too can cut and paste like a champ.

Topic Closed

This topic has been closed to new replies.

About this Topic