How to list all categories in wordpress menu automatically?

0

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

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

Any way to get 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>

Image panel admin wordpress:

    
asked by anonymous 09.10.2018 / 13:20

1 answer

0

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 look at the description of each array item here at wordpress documentation

    
11.10.2018 / 19:42