Last active
May 27, 2018 04:22
-
-
Save nmyers/d54da681e1908b7a8e0a to your computer and use it in GitHub Desktop.
Submenu nav - wordpress #wp
This file contains hidden or 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
// add hook | |
add_filter( 'wp_nav_menu_objects', 'my_wp_nav_menu_objects_sub_menu', 10, 2 ); | |
// filter_hook function to react on sub_menu flag | |
function my_wp_nav_menu_objects_sub_menu( $sorted_menu_items, $args ) { | |
// global $wp_query; | |
// console_debug('post is single >>',is_single() ? 'yes' : 'no'); | |
//console_debug('menu',$sorted_menu_items); | |
if (is_single()) { | |
$cat = get_the_category(); | |
$title = '---'; | |
if (isset($cat)) { | |
$title = $cat[0]->name; | |
} | |
//console_debug('cat',get_the_category()); | |
// if it's a post .. select the correct menu item | |
foreach ( $sorted_menu_items as $menu_item ) { | |
//console_debug('menuitem',$menu_item); | |
if (strtolower($menu_item->title)=='media centre') { | |
//$menu_item->title='MEDMED'; | |
$menu_item->current = false; | |
$menu_item->current_item_parent = true; | |
$menu_item->current_item_ancestor = true; | |
$menu_item->classes= array("menu-item","menu-item-type-post_type","menu-item-object-page","current-menu-ancestor","current-menu-parent","current_page_parent","current_page_ancestor","menu-item-has-children"); | |
} elseif (strtolower($menu_item->title)==strtolower($title)) { | |
//$menu_item->title='MEDMED'; | |
$menu_item->current = true; | |
$menu_item->current_item_parent = false; | |
$menu_item->current_item_ancestor = false; | |
$menu_item->classes = array("menu-item","menu-item-type-taxonomy","menu-item-object-category","current-menu-item"); | |
//$menu_item->current_item_ancestor = true; | |
} else { | |
$menu_item->current = false; | |
$menu_item->current_item_parent = false; | |
$menu_item->current_item_ancestor = false; | |
$menu_item->classes = array("menu-item"); | |
} | |
} | |
} | |
if ( isset( $args->sub_menu ) ) { | |
$root_id = 0; | |
// find the current menu item | |
foreach ( $sorted_menu_items as $menu_item ) { | |
if ( $menu_item->current ) { | |
// set the root id based on whether the current menu item has a parent or not | |
$root_id = ( $menu_item->menu_item_parent ) ? $menu_item->menu_item_parent : $menu_item->ID; | |
break; | |
} | |
} | |
// find the top level parent | |
if ( ! isset( $args->direct_parent ) ) { | |
$prev_root_id = $root_id; | |
while ( $prev_root_id != 0 ) { | |
foreach ( $sorted_menu_items as $menu_item ) { | |
if ( $menu_item->ID == $prev_root_id ) { | |
$prev_root_id = $menu_item->menu_item_parent; | |
// don't set the root_id to 0 if we've reached the top of the menu | |
if ( $prev_root_id != 0 ) $root_id = $menu_item->menu_item_parent; | |
break; | |
} | |
} | |
} | |
} | |
$menu_item_parents = array(); | |
foreach ( $sorted_menu_items as $key => $item ) { | |
// init menu_item_parents | |
if ( $item->ID == $root_id ) $menu_item_parents[] = $item->ID; | |
if ( in_array( $item->menu_item_parent, $menu_item_parents ) ) { | |
// part of sub-tree: keep! | |
$menu_item_parents[] = $item->ID; | |
} else if ( ! ( isset( $args->show_parent ) && in_array( $item->ID, $menu_item_parents ) ) ) { | |
// not part of sub-tree: away with it! | |
unset( $sorted_menu_items[$key] ); | |
} | |
} | |
return $sorted_menu_items; | |
} else { | |
return $sorted_menu_items; | |
} | |
} |
This file contains hidden or 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 wp_nav_menu( array( | |
'menu' => 'Header', | |
'container' => false, | |
'menu_class' => 'sidemenu', | |
'sub_menu' => true, | |
'direct_parent' => true, | |
'show_parent' => true | |
) ); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds submenu functionality