Skip to content

Instantly share code, notes, and snippets.

@digisavvy
Created September 28, 2012 15:30
Show Gist options
  • Save digisavvy/3800535 to your computer and use it in GitHub Desktop.
Save digisavvy/3800535 to your computer and use it in GitHub Desktop.
Custom Secondary Menu based on Bill Erickson's Code
/**
* Section Menu
* Displays the subpages of subpages of the current section
*
* @author Bill Erickson
* @link http://www.billerickson.net/custom-secondary-menu
*/
function be_section_menu_2() {
// Only run on pages
if( !is_page() )
return;
global $post;
$level = count( $post->ancestors );
// Only build tertiary menu if current page is at least third level
if( 1 > $level )
return;
$section_id = $post->ancestors[$level - 2];
// Get all the menu locations
$locations = get_nav_menu_locations();
// Find out which menu is in the 'primary' location
$menu = wp_get_nav_menu_object( $locations[ 'primary' ] );
// Grab all menu items in this menu that have a parent of the current section.
// This grabs the subpages, assuming the current section is a top level page
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'post_parent' => $section_id ) );
// If there are menu items, build the menu
if( !empty( $menu_items ) ) {
echo '<div class="sidenav"><ul class="section-submenu">';
$first = true;
foreach( $menu_items as $menu_item ) {
$classes = 'page-item';
// This adds a class to the first item so I can style it differently
if( $first )
$classes .= ' first-menu-item';
$first = false;
// This marks the current menu item
if( get_the_ID() == $menu_item->object_id )
$classes .= ' current_page_item';
echo '<li class="' . $classes . '"><a href="' . $menu_item->url . '">' . $menu_item->title . '</a></li>';
}
echo '</ul></div>';
}
}
add_action( 'genesis_before_sidebar_widget_area', 'be_section_menu_2' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment