wp_get_nav_menu_items how to exclude sub level menu items?

The question:

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_itemsto 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;
    }

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Think I worked it out!!

I did a print_r on each $menu_item and saw there’s an array key called menu_item_parent in there.

So I changed this:

foreach ( (array) $menu_items as $key => $menu_item ) {
    $title = $menu_item->title;
    $url = $menu_item->url;
    $menu_output .= '<option value="' . $url . '">' . $prefix . $title . '</option>';
}

to this:

foreach ( (array) $menu_items as $key => $menu_item ) {
    if ( $menu_item->menu_item_parent == 0 ) :
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_output .= '<option value="' . $url . '">' . $prefix . $title . '</option>';
    endif;
}

Now it will only pull menu items without a menu item parent.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment