Remove div and ul from wp_nav_menu

I looked and looked for ways to remove div and ul from wp_nav_menu() and I finally found a way.

The Code

<?php wp_nav_menu(array(
   'menu' => 'Main Menu', 
   'container' => false
)); ?>

So it all starts with the wp_nav_menu function, which I generally use to display my menus.

The $args

There is a whole list of arguments that you can use for the function and as you can see I’ve only used three. The menu argument dictates which menu is going to show up. I have a menu saved as Main Menu, so if I have that in my arguments, that particular menu will show up.

Next is ‘items_wrap’ which I still don’t fully understand. If someone else knows better than me, seriously, let me know in the comments and I will thank you profusely. The way I understand it is that %3$s just tells WordPress, “hey, this is where the list of items are going to be. Feel free to wrap ’em.” In this case we are refusing to wrap them with anything, thus, no <ul> are being used.

To explain what the Codex says, let’s take a look at the individual parts of ‘items_wrap’.

“Evaluated as the format string argument of a sprintf() expression. The format string incorporates the other parameters by numbered token. %1$s is expanded to the value of the ‘menu_id’ parameter, %2$s is expanded to the value of the ‘menu_class’ parameter, and %3$s is expanded to the value of the list items. If a numbered token is omitted from the format string, the related parameter is omitted from the menu markup.
Default: <ul id=”%1$s” class=”%2$s”>%3$s</ul>”

Breaking it down further:

Seems to make sense when we break it down.

Moving forward to ‘container’, the default is div and setting it to false prevents any extra container from showing. I get the same result by just using ‘container’ => ”, but the Codex says to set it to false, and who am I to argue.

That’s it. We’ve broken down the full array and now understand a little more about how to remove div and ul from wp_nav_menu. Like I said, if you have more insight, please feel free to share. Otherwise, check out some more WordPress functions: Custom Taxonomy Category Dropdowns or Custom Excerpt Lengths.

Notify of
Newest Most Voted
Inline Feedbacks
View all comments
7 years ago

I’ve tried ‘container’ => false and ‘container’ => ‘false’ and ‘container’ => ” and none of them have any affect on the surrounding div or ul 🙁 Found a million solutions online and none of them are working for me. Getting increasingly frustrated……

7 years ago

Are you using a custom theme or is it pre-made. Pre-made sometimes adds to the menu no matter what with JavaScript.

6 years ago

after searching atleast 5 hours, pasted 100s of code, i found this. Thankew so much.

5 years ago

this code is working well thanks

5 years ago

Work like a charm 🙂 Thanks for share.

John Hartley is a Director of Product Engineering at Beam Dental in Columbus, OH. With 5+ years of leadership experience he has worked in startups, agencies, and began his career as a freelance Front End Developer. Always looking to iterate, this blog is a place for him to share his knowledge as well as hone his craft, challenge assumptions, and build a strong base of leadership and management knowledge. Connect with him on LinkedIn

Would love your thoughts, please comment.x