WordPress.org

Ready to get started?Download WordPress

Forums

wp_nav_menu getting rid of div (23 posts)

  1. mloberg
    Member
    Posted 4 years ago #

    I'm trying to implement wp_nav_menu in my wordpress theme. I have a problem though, I don't want it wrapped in a div, I want it wrapped in just a ul. How do I do this?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 4 years ago #

    http://codex.wordpress.org/Function_Reference/wp_nav_menu

    shows the available args

    I haven't worked much with this yet, but it looks like you would play around with the format parameter? Or the container actually? Somethin to go on anyway.....

  3. mloberg
    Member
    Posted 4 years ago #

    I was messing with it and I couldn't get anything to change. It was frustrating.

  4. bcdunn619
    Member
    Posted 4 years ago #

    I would love to assist with this issue, however, I need to know what you are trying to accomplish in order to help.

  5. esmi
    Forum Moderator
    Posted 4 years ago #

    Why is the wrapper div a problem?

  6. mloberg
    Member
    Posted 4 years ago #

    I built a theme. The top nav bar is wrapped in a div of links. I was using wp_list_pages, with the addition of a "hard coded" home page.
    I now want to use wp_nav_menu. When I replaced wp_list_pages with wp_nav_menu, it was already inside a ul, because I had that hard coded home button.

    So what I'm trying to do is get rid of the div and ul so I can keep the hard coded home. I suppose I could also get rid of the hard coded home and just put that in the custom nav, right? But then I would need to changed the name of the wp_nav_menu div to links. I tried this, but I could not get it to work.

  7. esmi
    Forum Moderator
    Posted 4 years ago #

    Is this a vertical or horizontal menu? I'm just wondering if you could sidestep the problem using CSS.

  8. mloberg
    Member
    Posted 4 years ago #

    Horizontal. I could sidestep the problem if I could rename the div to links. But I couldn't get it, maybe I was typing something wrong.

    wp_nav_menu( array('container_id' => 'links') ); I tried this and it didn't work.

  9. esmi
    Forum Moderator
    Posted 4 years ago #

    What was the output?

  10. mloberg
    Member
    Posted 4 years ago #

    It doesn't change at all. It's wrapped in a div called menu and then a ul.

  11. Jack Reichert
    Member
    Posted 4 years ago #

    I've been playing with it as well, similar problem. I'm working with the most current beta.

    I enabled the menu in functions:
    add_theme_support( 'nav-menus' );

    In my theme page where I wanted to menu I put:
    wp_nav_menu(array('menu' => 'Main Dropdown Nav', 'container' => 'nav', 'container_id' => 'mainnav' ));

    It did change the container to nav but there was not id for that container.

    This should probably be moved to the beta forum.

    Thanks!

  12. greatdanemaniac
    Member
    Posted 4 years ago #

    mloberg: Can you please tell me how your CSS code for the menu looks like? I'd really like to build a theme myself compatible with WP 3.0, but I don't know how I should make the menu in CSS. I would be really happy if you could share that information with me.

    You can contact me either here, or by my website. Thanks!

    //Anna-Maria

  13. rickcurran
    Member
    Posted 4 years ago #

    I am experiencing the same thing, very few of the options args for wp_nav_menu seem to work. I've tried it in my own theme as well as in the new Twenty Ten them as well.

    Args either don't work or work differently than the reference page states at http://codex.wordpress.org/Function_Reference/wp_nav_menu

    menu_class: CSS class to use for the ul element which forms the menu

    However, setting this sets a CSS class on the containing div, despite the fact that container_class is supposed to be the argument that affects this.

    Other than that I'm finding other args just don't work, echo for instance has no effect. Is this something everyone is encountering and therefore a bug or is there likely a problem with my installation?

    Thanks :)

  14. rickcurran
    Member
    Posted 4 years ago #

    Ok, a combination of user error here and perhaps the Codex reference being a little unclear ;)

    I finally got the code to work the way I wanted, I used this code to bring in my menu:

    <?php
            $args = array( 'menu' => 'mainnav', 'menu_class' => 'container_12', 'menu_id' => 'navigation', 'container' => false, 'theme_location' => 'primary-menu');
            wp_nav_menu($args);
    ?>

    Basically this gets rid of the container div, puts a class and an ID on the UL of the menu and also specifies the menu location for use in the theme.

  15. James Mehorter
    Member
    Posted 4 years ago #

    Hi all, I am having the same problem as rickrurran. All I want is the menu to be within <div id="Navigation"> but the arguments passed to wp_nav_menu() have no affect on the output. Setting menu_class is the only argument which works to style the output :( Any ideas? Thanks!

    <?php wp_nav_menu( array( 'menu' => 'Masthead Navigation', 'container_id' => 'Navigation' ) ); ?>
    
    Generates the following:
    <div class="menu">
    
    <ul>
            <li class="page_item page-item-4 current_page_item">
              <a href="" title="Home">Home</a>
    
    </ul>
    </div>
  16. davidz.stein
    Member
    Posted 4 years ago #

    Hey
    I am having a similar problem to all you guys but I need to get rid of the menu_id or override it because it is preventing the list from appearing for me.
    How do I do this, its driving me crazy!

    Thanks

  17. phil_m_parsons
    Member
    Posted 3 years ago #

    I was having the same problem earlier until I had a quick dig around in the code (nav-menu-templste.php). Basically, I hadn't created the menu in the admin panel so it just ignored most of the args I sent in and defaulted to the pages menu.

    Not saying you guys are as stupid as me, but you may want to check that the nav menus and locations you have registered are actually set up?

  18. timfernihough
    Member
    Posted 3 years ago #

    I ended up having exactly the same problem here and phil_m_parsons hit the nail on the head.

    I was provided the theme by someone else who had worked on the project before me and it was already "enabled" as a theme in wordpress. I went ahead and created page after page of content and noticed it was populating my menu like it was supposed to. Nice.

    The not nice thing was that the styling was missing. I realized I needed to mess around with the parameters you pass into wp_nav_menu() and got absolutely nowhere as it was ignoring most of my parameters including the "container_id" parameter.

    Turns out you have to go to Appearance -> Menus and actually create your "menu" entity. If you don't do this (and set "menu" parameter to the name you choose for your menu), then WordPress just defaults to the wp_list_pages() function because it can't find your menu. This is why it ignores most of the parameters.

  19. xinaesthetic
    Member
    Posted 3 years ago #

    I've just made the same mistake, and am inclined to say this doesn't indicate either myself or anyone in this thread is stupid... this is a usability bug IMHO.

    $menu_class
    (string) (optional) CSS class to use for the ul element which forms the menu
    Default: menu

    does not help much when everything seems to be working ok but the class applies to the div rather than the ul.

    Personally, I'd like to have a menu similar to the one produced by wp_list_pages to be created automatically somehow... it seems like on the Menus admin page, I can add all of the pages easily but I can't then see how they relate in the original hierarchy (ie, the page parents), let alone have it automatically recreated. There should perhaps be an option on pages in the menu admin to 'include subpages'.

    Not that if I was in charge it would all be perfect ;)

    I don't know how constructive it really is posting this here, but I don't seem to be breaking any rules resuscitating an old thread... and I've got it off my chest.

  20. fireundubh
    Member
    Posted 3 years ago #

    In case anyone is still looking for a way to remove the container and unordered list elements:

    $m = wp_nav_menu(array('echo' => '0')); echo strip_tags($m, '<a>');

    This code stores the menu in the $m variable. The strip_tags() function then removes all HTML tags from the stored menu, except for the links. This effectively eliminates the need for inline styling, at least under specific circumstances.

  21. gavinr
    Member
    Posted 3 years ago #

  22. gohlkus
    Member
    Posted 3 years ago #

    I was struggling with this somewhat as well -- I would echo everything in xinaesthetic's post from four months ago.*

    I tried to use wp_nav_menu( array( 'container' => false, 'menu_class' => 'nice-menu' ) ); to have the automatically generated page menu show up without a div, with a menu_class applied to the ul, and with the subpages automatically populated. The documentation implies that this should work; however, it doesn't explain why it doesn't work. timfernihough and phil_m_parsons did a good job above helping me understand what was happening, but there doesn't seem to be a good reason for it to work that way.

    I'm making this site for non-techie colleagues and, frankly, what would be easiest for everyone (techie or not) is to have the hierarchy of pages just show up in the menu, styled the way I want. That's it. For fairly obvious reasons.

    The thing that makes no sense to me is that the menu of last resort defaulted to by wp_nav_menu does not accept the parameters given in wp_nav_menu. Very strange.

    The other thing is this: what is the point of having pages able to be set up hierarchically if that hierarchy doesn't fully integrate with the menu system by default? Why bother creating a hierarchy of pages, and a page order, if you just have to re-create it in the menu interface every time you change something, just to be able to use a few parameters?

    The workaround I chose for now was actually just to use wp_list_pages. Unfortunately, that means that whatever changes are required (e.g. customizing the presentation of any of the particular list items) I just have to do in header.php, making it one more thing I have to do myself, rather than have it be in the custom menu interface so that my co-workers can customize it.

    And the undocumented parameter in the site gavinr links to above 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>' still wouldn't seem to apply, I presume, in wp_nav_menu when you don't have an actual custom menu selected.
    ________________________
    * (Everything except that I have no doubt this post is constructive -- I seem to remember that creating a new thread without looking to see if there's an old one first is discouraged.)

  23. Zambrano Sergio
    Member
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic