How to set limit only on top levels of wp_nav_menu?

According to the codex, you just need to use depthparameter to display only top-level pages :

<?php wp_nav_menu( array( 'location' => 'your_location', 'depth' => 1 ) ); ?>

For more reference see this.

You could also fix both of your problem by using [wp_get_nav_menu_items][2] and then using a custom loop to parse only first and top-level pages.

EDIT***
I did spent some time starting something to help, unfortunately I cannot finish it right now, and maybe could make it more elegant, but that could be a start :

<?php 
    $menu_name="principal";
        if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
        $menu_test = wp_get_nav_menu_object( $locations[ $menu_name ] );
                    // WE GET THE ITEMS IN menu_order
        $menu_test_items = wp_get_nav_menu_items( $menu_test->term_id );
        $top_items = array();
                    // LOOP THEM AND CREATE NESTED ARRAY LIKE THE MENU 
        foreach( $menu_test_items as $menu_test_item ) {
            if ( $menu_test_item->menu_item_parent == 0 ) {
                $top_items[$menu_test_item->ID][$menu_test_item->ID] = $menu_test_item;     
            } else {
                $top_items[$menu_test_item->menu_item_parent][$menu_test_item->ID] = $menu_test_item;
            }

        }
                    // THEN WE COULD JUST LOOP IT x TIMES AND BREAK
        foreach ( $top_items as $top_item ) {  
            // Npw you need to loop x times
            // and display $top_item with another foreach
        }
    }
?>

Leave a Comment