scriptygoddess

09 Oct, 2010

wp_nav_menu and 'container' => false appears to not work

Posted by: Jennifer In: WordPress|WordPress: Lessons Learned

A quick post to deal with a problem I've come across again and again. I think I see the issue now, and wanted to make a note of what I *think* I'm seeing. I've also seen some complain about this same problem too.

When you use wp_nav_menu() to create your nav bar, it will automatically add a container div that wraps around your navigation (which is presented in an unordered list). However, maybe you don't want that div there. According to the codex, you should only have to create your nav with 'container' => false to get that container to go away, but for some reason, on what was seeming like random occasions, it would have no affect.

Well, I realized that the reason for this is because I had not yet set up my menu specifically. As soon as I went into the admin, created a menu, and assigned it to the same location wp_nav_menu() was calling, voila! The container went away.

I understand that if there is no menu set up yet, that wp_nav_menu() resorts to calling wp_list_pages(), but it should still pay attention to the arguments passed to the original function. However, it definitely seems like it is having issues. Not sure if that's a bug or not, but now I know (or at least I THINK I know) what's happening and how to fix it.

Reference: WordPress 3.0.1

5 Responses to "wp_nav_menu and 'container' => false appears to not work"

1 | Andrew Nacin

October 10th, 2010 at 8:12 pm

Avatar

You can turn off the fallback. Try this:

wp_nav_menu( 'theme_location' => 'your-location', 'container' => false, 'fallback_cb' => false );

2 | Andrew Nacin

October 10th, 2010 at 8:28 pm

Avatar

I made a change in the code to make this functionality more explicit: http://core.trac.wordpress.org/changeset/15772.

I also updated the Codex page: http://codex.wordpress.org/Function_Reference/wp_nav_menu#Parameters.

3 | Amit

October 11th, 2010 at 7:49 am

Avatar

try 'container' => ", works for me.

4 | Jennifer

October 11th, 2010 at 7:59 am

Avatar

@Amit – that doesn't work for me when a menu isn't set up.

@Andrew – so that means the menu will not display at all (and wp_list_pages won't even run) if no menu is specified…? I guess that works – but I still think it should be capable of doing the fallback and not start adding code that's not even part of the fallback function (that container div stuff isn't part of wp_list_pages…)

5 | Ben

March 29th, 2012 at 7:19 am

Avatar

Oh snap! You're totally right! Thanks for passing on the knowledge 😉

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements