• Resolved jerreds

    (@jerreds)


    I am trying to use the wp_nav_menu() function to set classes on a menu, and I thought I was following the documentation exactly, but I’m not getting what I expected. Here is the code:

    <?php 
    	  wp_nav_menu( array(
    			'theme_location' => 'primary',
    			'container' => 'nav',
    			'container_class' => "collapse navbar-collapse",
    			'menu_class' => "navbar-nav",
    			'menu_item' => 'nav-item',
    		) );
    ?>

    I expected a nav with the classes in container_class that contained a ul with the classes in menu_class.

    But what I get is a <nav class="navbar-nav">, inside it is a ul with no classes.
    Nothing has the classes collapse navbar-collapse.

    Is anyone able to explain why it is overwriting like this, or what I have wrong here?

    • This topic was modified 2 years, 10 months ago by jerreds.
Viewing 5 replies - 1 through 5 (of 5 total)
  • I would expect the same thing. Perhaps there is code with filters affecting the menu? There are several that could change things: https://developer.wordpress.org/reference/functions/wp_nav_menu/

    There is no parameter called 'menu_item'.

    Thread Starter jerreds

    (@jerreds)

    You know what, I think I removed whatever it was that had it not working, it’s doing what I put from before. So thanks, I guess! The only thing I did was change how the menu is registered… Not just browser cache or anything, because I definitely double-checked all of that prior to posting.

    …And thanks, I removed the menu item thing, I was looking at the classes on accident.

    As a note for this… I’ve seen this happen too with some menus that I’ve done, and the answer was that I had not set a menu in the admin area to be dipslayed in that menu location. That had WordPress whot eh fallback “default” menu structure, and that doesn’t take the same classes, etc, as it does when you set the menu correctly.

    @catacaustic thanks for the tip.

    This should be addressed though, because, as a developer adding to the theme incrementally I have added the code in a template and it should be honored.

    I should be able to add the menu through my template or functions so it is does not require end user to create.

    Can you or an admin please post a response with the WordPress way to do this?

    • This reply was modified 2 years, 5 months ago by Mike Ritter.

    **How to Avoid**

    Thanks @catacaustic for starting me on the path to resolving this.

    The solution is the parameter fallback_cb.

    In your array, set this option to ‘false’ or ‘0’.

    
    <?php wp_nav_menu(['location'=>'header-menu','fallback_cb'=>'0']) ?>
    

    See https://developer.wordpress.org/reference/functions/wp_nav_menu/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using wp_nav_menu function to set classes’ is closed to new replies.