How do I retrieve menu items from one level only?

The WordPress function wp_get_nav_menu_items returns my whole site’s navigation nested and all. Now if i want to get the nav menu for one “level” of that nested navigation only, how do I do that?

Related posts

Leave a Reply

2 comments

  1. If you want to return an array of items (not output like wp_nav_menu), you can try this:

    $menu_name = 'your_menu_location';
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
    
        $menu_items = wp_get_nav_menu_items($menu->term_id);
    
        foreach ( (array) $menu_items as $key => $menu_item ) {
            if ($menu_item->menu_item_parent != 0 ) continue;
            $title = $menu_item->title;
            $url = $menu_item->url;
    
            DO WHAT EVER YOU WANT HERE 
    
        }
    }   // endif has nav menu or not