Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created June 19, 2012 14:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/2954640 to your computer and use it in GitHub Desktop.
Save billerickson/2954640 to your computer and use it in GitHub Desktop.
<?php
/**
* Section Menu
* Displays the subpages of the current section
*
* @author Bill Erickson
* @link http://www.billerickson.net/custom-secondary-menu
*/
function be_section_menu() {
// Only run on pages
if( !is_page() )
return;
// If top level page, use current ID; else use highest ancestor
global $post;
$section_id = empty( $post->ancestors ) ? $post->ID : end( $post->ancestors );
// 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 '<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>';
}
}
add_action( 'genesis_before_loop', 'be_section_menu' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment