Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
    }
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
If this is the solution, mark it solved. –  m0r7if3r Jan 27 '12 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. –  Johannes Jan 27 '12 at 17:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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