How to list all categories in wordpress menu automatically?

Asked

Viewed 684 times

1

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

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

Some 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>

Image panel admin wordpress:

inserir a descrição da imagem aqui

1 answer

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.