Last active
December 15, 2015 05:39
-
-
Save ChrisMBarr/5210374 to your computer and use it in GitHub Desktop.
Wordpress Menu Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav class="container_12"> | |
<?php | |
//Counter | |
$i=0; | |
//An array that will eventually contain the correct top-level navigation items in it | |
$menu_arr = array(); | |
//Loop though all the nav items in Wordpress | |
foreach ( wp_get_nav_menu_items('Nav') as $key => $item ) { | |
//Depending on the item type, determine if it is currently selected | |
$is_current = | |
( $item->object == 'page' && is_page($item->object_id) ) || | |
( $item->object == 'custom' && ( is_home($item->object_id) || is_category() || is_single() ) ); | |
//Create an array representation of this nav item | |
//that includes some important properties we will need. | |
//The first item needs a special grid class to be positioned correctly | |
$this_nav_item = array( | |
'url' => ($item -> url), | |
'title' => ($item -> title), | |
'grid_class' => (($i==0) ? 'grid_2 prefix_1' : 'grid_2'), | |
'is_current' => $is_current | |
); | |
if($item -> menu_item_parent == 0){ | |
//Only add top-level items to the main array | |
$menu_arr[$item->ID] = $this_nav_item; | |
}else{ | |
//If a sub-page is the current page, find the parent page and make it selected instead | |
if($is_current ) $menu_arr[$item -> menu_item_parent ]['is_current']= true; | |
} | |
//Increment the counter | |
$i++; | |
} | |
//---------------------- | |
//Find the first item in the array | |
$firstItem = array_shift(array_values($menu_arr)); | |
//If we don't know where we are, then just make the first item as the current one | |
if(!is_home() && !is_page()) $firstItem['is_current'] = true; | |
//Var to stick some output HTML into | |
$menu_html=""; | |
//Loop through the array we created above... | |
foreach ($menu_arr as $key => $item) { | |
//If the item is the current one, add a class to make it visibly selected | |
$selected = $item['is_current'] ? ' class="nav-current"' : ''; | |
//Build up the needed output | |
$menu_html.= '<div class="' . $item['grid_class'] . '"><a href="' . $item['url'] . '"'.$selected.'>' . $item['title'] . '</a></div>'; | |
} | |
//Output the final menu HTML | |
echo $menu_html; | |
?> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<nav class="container_12"> | |
<div class="grid_2 prefix_1"><a href="http://chris-barr.com/" class="nav-current">Blog</a></div> | |
<div class="grid_2"><a href="http://chris-barr.com/photos/">Photos</a></div> | |
<div class="grid_2"><a href="http://chris-barr.com/videos/">Videos</a></div> | |
<div class="grid_2"><a href="http://chris-barr.com/projects/">Projects</a></div> | |
<div class="grid_2"><a href="http://chris-barr.com/about/">About</a></div> | |
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$isPageWithSubpagesOrASubPage = | |
is_page() && ( | |
get_pages('child_of='.$post->ID) || ( | |
end($post->ancestors) != 0 && | |
get_pages("child_of=".end($post->ancestors)) | |
) | |
); | |
//Show the sidebar if this is a page/sub-page | |
$showSidebar = $isPageWithSubpagesOrASubPage; | |
if ($showSidebar) : | |
?> | |
<div class="grid_3 push_9"> | |
<?php get_sidebar(); ?> | |
</div> | |
<?php endif; ?> | |
<div class="<?php echo $showSidebar ? 'grid_9 pull_3' : 'grid_12'; ?>"> | |
<div id="content"> | |
<!-- Content goes here --> | |
</div> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment