Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created June 30, 2011 12:43
Show Gist options
  • Save mattboon/1056146 to your computer and use it in GitHub Desktop.
Save mattboon/1056146 to your computer and use it in GitHub Desktop.
WordPress - Tree Navigation
<nav role="navigation">
<ul>
<?php foreach($primary_nav_items as $level_one) :
/* LOOP LEVEL ONE */
$level_two = get_pages('child_of='.$level_one.'&parent='.$level_one.'&sort_column=menu_order&sort_order=asc&exclude='.$excluded_pages_string.'');
/* GET ALL LEVEL TWO PAGES */ ?>
<li<?php if($root_page_id==$level_one): ?> class="current<?= $level_two ? ' open' : '' ?>"<?php endif; ?>>
<a href="<?= get_permalink($level_one); ?>">
<?= get_the_title($level_one); ?>
</a>
<?php if ($level_two && $root_page_id==$level_one):
/* TEST FOR LEVEL TWO PAGES */ ?>
<ul>
<?php foreach($level_two as $level_two_page):
/* LOOP LEVEL TWO */
$level_three = get_pages('child_of='.$level_two_page->ID.'&parent='.$level_two_page->ID.'&sort_column=menu_order&sort_order=asc&exclude='.$excluded_pages_string.'');
/* GET ALL LEVEL THREE PAGES */
$level_two_current=0;
$show_level_three=0;
/* SET VARS */
if($level_two_page->ID==$post->ID) {
$level_two_current=1;
}
if($level_three && $level_two_current==1 || $level_two_page->ID==$post->post_parent) {
$show_level_three=1;
}
?>
<li<?php if($level_two_current==1 || $show_level_three==1): ?> class="current<?= $show_level_three==1 ? ' open' : '' ?>"<?php endif; ?>>
<a href="<?= get_page_link($level_two_page->ID); ?>">
<?= $level_two_page->post_title; ?>
</a>
<?php if ($show_level_three==1):
/* TEST FOR LEVEL THREE PAGES */ ?>
<ul>
<?php foreach($level_three as $level_three_page):
/* LOOP LEVEL THREE */ ?>
<li<?= $level_three_page->ID==$post->ID ? ' class="current"' : '' ?>>
<a href="<?= get_page_link($level_three_page->ID); ?>">
<?= $level_three_page->post_title; ?>
</a>
</li>
<?php endforeach;
/* END LOOP LEVEL THREE */ ?>
</ul>
<?php endif;
/* END TEST FOR LEVEL THREE PAGES */ ?>
</li>
<?php endforeach;
/* END LOOP LEVEL TWO */ ?>
</ul>
<?php endif;
/* END TEST FOR LEVEL TWO PAGES */ ?>
</li>
<?php endforeach;
/* END LOOP LEVEL ONE */ ?>
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment