How to list all categories in wordpress menu automatically?

I'm trying to list all wordpress categories to form a dropdown menu with them as the menu is created in the theme admin panel.

I want that where the categories item is in the menu of the image below are listed all the categories of the site automatically, but with the possibility of the user being able to choose in which position the category item will be in the menu.

Any way to achieve this result?

Functions.php:

function register_my_menu() {
    register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );

function register_my_menus() {
    register_nav_menus(
        array(
            'header-menu' => __( 'Header Menu' )
        )
    );
}
add_action( 'init', 'register_my_menus' );

Header.php:

<nav class="small-12 large-8 show-for-large cell">
    <?php $menu = str_replace('sub-menu', 'menu', wp_nav_menu( array(
        'echo' => false,
        'theme_location' => 'header-menu',
        'items_wrap' => '%3$s' 
    ))); 
    $categorias = get_categories(array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ));
    ?>
    <ul class="dropdown menu" data-dropdown-menu>
        <?php  echo $menu; ?>

        <li><a href="#">Categorias</a>
            <ul class="menu">
            <?php foreach ($categorias as $categoria) {
                printf( '<a href="%1$s">%2$s</a><br />',
                    esc_url( get_category_link( $categoria->term_id ) ),
                    esc_html( $categoria->name )
                );
            }
        ?>
            </ul>
        </li>
    </ul>
</nav>

Wordpress admin panel image:

insert the description of the image here

Author: Bruno Folle, 2018-10-09

1 answers

To enable the submenu in wordpress after registering the menu in functions, you add in the array of wp_nav_menu( array( 'depth' => 1 )); By default it comes 0 any questions just see the description of each array item here in the wordpress documentation

 0
Author: Eduardo, 2018-10-11 17:42:59