22

I am using the new menu system of Wordpress, and here is the result of wp_nav_menu()

    <div class="menu-main-menu-container">
   <ul id="menu-main-menu" class="menu">
   <li id="menu-item-28" class="menu-item menu-item-type-post_type current-menu-item page_item page-item-21 current_page_item menu-item-28"><a href="http://www.bemang.com/">Trang nhà</a></li>
    <li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-29"><a href="http://www.bemang.com/blog/">Blog</a></li>
    <li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-30"><a href="http://www.bemang.com/gioi-thieu/">Giới thiệu</a></li>
    </ul>
   </div>

What I want is only this:

<li id="menu-item-28" class="menu-item menu-item-type-post_type current-menu-item page_item page-item-21 current_page_item menu-item-28"><a href="http://www.bemang.com/">Trang nhà</a></li>
   <li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-29"><a href="http://www.bemang.com/blog/">Blog</a></li>
   <li id="menu-item-30" class="menu-item menu-item-type-post_type menu-item-30"><a href="http://www.bemang.com/gioi-thieu/">Giới thiệu</a></li>

No, wrapping div or ul, I have been trying to few ways and google around but no result :(

30

The wrapping div is easy. When you use wp_nav_menu(), add this argument: 'container' => false

For the <ul> element, there's an argument called 'items_wrap'. See woodchucky's answer for more info on that one (and upvote it!).

3
  • Thanks a lot man! It works now. I was not familiar with PHP and did not know that I need to put the add_filter to the functions.php.
    – Tuan Anh
    Oct 6 '10 at 23:11
  • 1
    That still doesn't explain how to remove the div, which rather wraps up the wrap_class explained above. The container argument set to false doesn't work.
    – sergio
    Apr 25 '11 at 21:50
  • 2
    If the container argument doesn't work, that means you're not using wp_nav_menu. You're probably running the fallback cb, which defaults to wp_page_menu(), by failing to specify either a menu, or a menu location with an assigned menu. Apr 26 '11 at 21:03
34

wp_nav_menu() accepts an undocumented parameter

'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>'

This is executed by: $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );

you can play around with the sprintf arguments

ex: 'items_wrap' => '%3$s' would remove the wrapping <ul> tag

1
  • Heh, cool! And welcome to WPSE @woodchucky!
    – fuxia
    Apr 25 '11 at 23:18
7

For outputting only li's add this to wp_nav_menu array:

'container'      => '',
'items_wrap'    => '%3$s',
3

'container' => false was failing for me because I was specifying a not existing 'theme_location' and 'menu'. It worked as soon as I fixed this.

Solution from: wordpress.org/support

1

To filter out the wrapping div:

add_filter('wp_nav_menu_args', 'prefix_nav_menu_args');
function prefix_nav_menu_args($args = ''){
    $args['container'] = false;
    return $args;
}
0

2021

Tested and Working ✔

To remove <div>

Use container => false or container => '' inside wp_nav_menu.

wp_nav_menu( 
    array( 
        'container' => false
    ) 
);

Still not working?

Do not add theme_location to your wp_nav_menu. If you do you will still see the <div> container.

wp_nav_menu( 
    array( 
        'container' => false,
        'theme_location' => 'Primary Menu'
    ) 
); 

To remove the <ul>

If you want to remove the <ul> wrapper, use this. Based on @Mill answer.

wp_nav_menu( 
    array( 
        'container' => '',
        'items_wrap' => '%3$s'
    ) 
); 

If you need control on <ul> elements

If you want to retain all elements inside the <ul> from orignal WordPress id and class use this 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>' based on @woodchucky answer.

wp_nav_menu( 
    array( 
        'container' => '',
        'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>'
    ) 
); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy