I have the following function to output a custom menu in select format for a responsive design I'm working on.
It's working exactly how I want it to apart from I would need to exclude sub menu items. At the moment it includes all levels.
Does anyone know how I can tell wp_get_nav_menu_items
to only display top level menu items please?
function jeMobileMenu( $args ) {
// Set up defaults arguments
$defaults = array (
'menuSlug' => '',
'placeholder' => 'Menu',
'prefix' => ' » ',
'navID' => 'mobileNav',
'navClass' => ''
);
// Parse incomming $args into an array and merge it with $defaults
$args = wp_parse_args( $args, $defaults );
//Declare each item in $args as its own variable i.e. $menuSlug, $placeholder
extract( $args, EXTR_SKIP );
// If no menu slug has been passed then lets bail
if ( empty($menuSlug) )
return;
// If the menu slug that has been passed doesn't correspond to an exisiting menu then lets bail
if ( !has_nav_menu( $menuSlug ) )
return;
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menuSlug ] );
$menu_items = wp_get_nav_menu_items($menu->term_id);
// Wrap the select in a nav element with passed id and classes
$menu_output = '<nav id="' . $navID . '" class="' . $navClass . '">';
$menu_output .= '<select id="menu-' . $menuSlug . '" onchange="window.open(this.options[this.selectedIndex].value,\'_top\')">';
// Add placeholder and home link
$menu_output .= '<option value="">' . $placeholder . '</option>';
$menu_output .= '<option value="' . home_url( '/' ) . '">' . $prefix . 'Home</option>';
// Now loop through all the menu items and create them as options in the select list
foreach ( (array) $menu_items as $key => $menu_item ) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_output .= '<option value="' . $url . '">' . $prefix . $title . '</option>';
}
$menu_output .= '</select>';
$menu_output .= '</nav>';
echo $menu_output;
}