• Resolved nathan12343

    (@nathan12343)


    Hi all,

    I want to provide back links on a site that has three levels of content (all pages not posts) eg.

    > Section 1
    > Page A
    > subpage i
    > subpage ii
    > subpage iii
    > Page B
    > subpage iv
    > subpage v
    > subpage vi
    > Section 2
    > Page C
    > subpage vii
    > subpage viii

    and so on.

    When I am on Page A I want to provide a link “Back to Section 1”

    If I am on subpage ii I want to provide links “Back to Section 1 / Back to Page A”

    I’m doing this for a mobile browser and would rather not use plugins etc for page weight. By using:

    <a href="<?php echo get_permalink($post->post_parent); ?>">Back to <?php echo get_the_title($post->post_parent); ?></a>

    I can get the link to the Section, but not the Page too.

    Any help greatly appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter nathan12343

    (@nathan12343)

    Indents didn’t work!!

    Section is the first level. Page is the second, subpage is the third.

    • section
    • Page
    • subpage

    Hope that worked better!

    Thread Starter nathan12343

    (@nathan12343)

    I realise that this question might not have made a lot of sense! But in case anyone is interested I have found the solution.

    The following code will display the links to the ancestors by working out how far away the current page is from the home page and displaying the necessary parent and grandparent links accordingly.

    My homepage is a static page that sits alongside the potential grandparents… did I make this too complicated?!

    <?php //parent variables
    	$parent = get_post($post->post_parent);
    	$parent_title = get_the_title($parent);
    	$grandparent = $parent->post_parent;
    	$grandparent_title = get_the_title($grandparent);?>
    
    	<?php // is the homepage the granparent?
    	if ($grandparent == is_page('0')) { ?>
    	<li><a href="/<?php echo get_permalink($grandparent); ?>">Back to <?php echo $grandparent_title; ?></a></li>
    	<li><a href="<?php echo get_permalink($post->post_parent); ?>">Back to <?php echo $parent_title; ?></a></li>
    
    	<?php // is the homepage the parent?
    	} elseif ($post->post_parent ==is_page('0')) {?>
       <li> <a href="<?php echo get_permalink($post->post_parent) ?>">Back to <?php echo $parent_title; ?></a></li>
    
        <?php // I must be a top level page!
    	} else { ?> <!-- no parent to show -->
    
        <?php }?>

    thank you 🙂

    Just what I needed – thank you for posting this!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Back to Parent. Back to Grandparent’ is closed to new replies.