Skip to content

Instantly share code, notes, and snippets.

@gabbsmo
Last active December 24, 2015 11:48
Show Gist options
  • Save gabbsmo/6793141 to your computer and use it in GitHub Desktop.
Save gabbsmo/6793141 to your computer and use it in GitHub Desktop.
<?php
function theme_wp_nav_menu_sub_menu_objects( $sorted_menu_items, $args ) {
if ( isset( $args->sub_menu ) ) {
$root_id = 0;
//var_dump($sorted_menu_items);
// 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;
}
}
$menu_item_parents = array();
foreach ( $sorted_menu_items as $key => $item ) {
if ( $item->current_item_ancestor ) $menu_item_parents[] = $item->ID;
// 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 {
// not part of sub-tree: away with it!
unset( $sorted_menu_items[$key] );
}
}
return $sorted_menu_items;
} else {
return $sorted_menu_items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment