4

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

1 Answer 1

8

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.

2
  • If this is the solution, mark it solved.
    – mor7ifer
    Jan 27, 2012 at 17:13
  • 1
    You can't mark your own answers as the solution until 2 days after the question was asked but will do it then.
    – Brigante
    Jan 27, 2012 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.