wordpress: back to (grand)parent

was challenged at work and couldn’t think of a proper solution. i knew it was possible, but didn’t know how to execute it

this is what happens when you don’t know all the tools you have available to you…

thank god for the wordpress community

background

this makes absolutely no sense without any background! essentially i have a splash page that goes to several sub-sites. when you get to a sub-site, i want each sub-site’s home to go back to its own home location, not the splash page, which would be

< ?php echo get_option('home'); ?>

code

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

Related Posts