wp_nav_menu getting rid of div
-
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?
-
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?
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.
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: menudoes 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.
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.
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.)[SOLVED]
I FOUND the problem.
I posted the solution in another post.
- The topic ‘wp_nav_menu getting rid of div’ is closed to new replies.