Created
June 19, 2012 14:55
-
-
Save billerickson/2954640 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* 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