The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

[resolved] How does wp_nav_menu work? (9 posts)

  1. NikiOnTime
    Posted 3 years ago #

    I'm new to the WP community and I'm trying to build my first theme.
    I'm having problems with the wp_nav_menu function.
    I want to customize the markup that is generated by it but so far I'm failing. I want to change the container to <nav>, to set class="" and id="" to it.
    in functions.php I've written:

    function register_menus() {
                    'header-menu' => __('Main Menu'),
                    'side-menu' => __('Side Menu'),
                    'footer-menu' => __('Footer Side Menu')

    in header.php:

    wp_nav_menu(array('theme_location' => 'header-menu',
    			'container' => 'nav',
    			'menu_id' => 'mainNav',
    			'menu_class' => 'ulmenu',

    The result is that only class="ulmenu" is applied to the <div> tag containing
    ul tag. What could possibly be the problem? I can change my CSS to make it work but I feel like if I don't figure this problem out it will snowball in the future.

    Thanks in advance!


  2. marcelle42
    Posted 3 years ago #

    To apply an id and class to the container element, you should use the 'container_id' and 'container_class' options. The other options may be trumping the 'container' => 'nav' option.

  3. NikiOnTime
    Posted 3 years ago #

    Thanks for the fast answer but it's still not working.
    If I write this in the header.php:

    wp_nav_menu(array('theme_location' => 'header-menu',
    			'container' => 'nav',
    			'container_id' => 'mainNav',
    			'container_class' => 'ulmenu'

    It generates the default menu:

    <div class="menu">
              <li class="page_item page-item-2">
                   <a href="http://localhost/CastingCartel/?page_id=2">Sample Page</a>
  4. marcelle42
    Posted 3 years ago #

    Can you use pastebin.com to paste your entire header.php file and share the link, for context? Do you have a link to your site that you can share?

  5. marcelle42
    Posted 3 years ago #

    Just having a conversation, and learned that wp_nav_menu() doesn't fire if you don't have any menus defined. Do you have any menus defined under Appearance > Menus?

  6. NikiOnTime
    Posted 3 years ago #


    At the moment I don't have hosting and I'm working on wamp server on my machine :/ but if you don't see any syntax or logic errors in the source what are the possible problems ?

  7. NikiOnTime
    Posted 3 years ago #

    I was thinking if I should add menu items ... Just a second

  8. NikiOnTime
    Posted 3 years ago #

    Thanks a lot marcelle42, you are right. Now it works 100% correct, all the attributes of the function are working. It was interesting that menu_class was working thou :D

  9. marcelle42
    Posted 3 years ago #

    Fantastic! Thanks for letting me know. Don't forget to mark this as resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic