WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. NikiOnTime
    Member
    Posted 1 year ago #

    Hello,
    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() {
        register_nav_menus(
                array(
                    '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!

    NikiOnTime

  2. marcelle42
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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">
         <ul>
              <li class="page_item page-item-2">
                   <a href="http://localhost/CastingCartel/?page_id=2">Sample Page</a>
              </li>
         </ul>
    </div>
  4. marcelle42
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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
    Member
    Posted 1 year ago #

    header.php
    functions.php

    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
    Member
    Posted 1 year ago #

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

  8. NikiOnTime
    Member
    Posted 1 year 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
    Member
    Posted 1 year 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