Support » How-To and Troubleshooting » Remove the div and ul tags from wp_nav_menu() function

Remove the div and ul tags from wp_nav_menu() function

  • Hello!

    I’ve tried to delete the div and ul tags from the function wp_nav_menu() by defining the parameter container as none but the div anf ul tags still showing.

    This is the whole code of my wp_nav_menu() function and parameters:

    'menu_class' => 'Navigation',
    'container' => 'none',
    'container_class' => 'menu-header',
    'theme_location' => 'primary'

    How can I resolve that issue?


Viewing 15 replies - 1 through 15 (of 27 total)
  • ‘container’ => ‘none’ will not work, you can use:

    <?php wp_nav_menu( array(
        'container' => ''
    )); ?>

    this will remove the container.



    It’s still wrapping the Navigation(even if I use just in your code without any other parameter).

    To remove <ul></ul> you can add this in your functions.php file.

    // Removes ul class from wp_nav_menu
    function remove_ul ( $menu ){
        return preg_replace( array( '#^<ul[^>]*>#', '#</ul>$#' ), '', $menu );
    add_filter( 'wp_nav_menu', 'remove_ul' );

    And just so that I understand correctly ‘container’ => ” was working right?

    No, it’s not removing the <ul></ul> tags.

    And no, the 'container' => '' wasn’t working at all.

    Note that I’m using on theme based on TwentyTen in WordPress 3.0.4 – if that is matter.

    <?php wp_nav_menu( array( 'container_class' => false, 'theme_location' => 'primary' ) ); ?>

    Before we did ‘container’ => ”, this time let’s make this ‘container_class’ => false this is changed code directly from TwentyTen.


    Doesn’t working too…

    Did you check this codes on WordPress demo?

    TwentyTen functions.php http://wordpress.pastebin.com/E7BrTA9U

    Take a look what I added in functions.php: from line 100 to 106

    It’s also doesn’t working…

    And I know that it should be work by WordPress Codex, but as I see it – it don’t.

    Did you tried your codes?
    Because I’ve checked this codes on 2 WordPress websites(on different servers) and I’ve got the same results.

    I can show you how I got this working on another theme while back. Both codes functions.php and header.php are located here. There are just codes, not the actual files, but you’ll get what you need.


    It’s like the theme doesn’t recognize the registered menu in your condition:

    <?php if ( has_nav_menu( 'primary-menu', 'mytheme' ) ) { ?>
    	<?php wp_nav_menu( array( 'container' => false, 'menu_class' => 'menu', 'theme_location' => 'primary-menu', 'fallback_cb' => 'display_home' ) ); ?>
    	<?php } else { ?>
        <ul class="menu">
    	<li><a href="<?php echo home_url(); ?>/">Home</a></li>
    	<?php wp_list_pages('title_li=' ); ?>
        </ul><!-- end .menu -->
    	<?php	} ?>

    It’s displayed the menu with wp_list_pages function like I don’t have any registered menu.
    (I’ve deleted all <li>function.php
    content and put just yours to avoid conflict so there is no way that other code can make some crossing with this thing).

    Go to http://wordpress.pastebin.com and paste your current functions.php and as well as separate paste from your header.php. So whatever you have in your theme, without any of codes I previously gave you.

    In above http://wordpress.pastebin.com/sYeiZZ2G you have the codes for functions.php and also the code for your header.php. Unless there is something else going on, my codes do work for sure. Let me give you live example: http://fireflyblog.org is using what I sent you and there are no problems at all. If you take all out the TwentyTen will not even function properly, as the rest of the codes are much needed for the theme to operate 🙂

    BTW what’s the URL to this?

    I see now that I didn’t wrote my last post right.

    I’ve put only your code and still it’s doesn’t work!(I’ve deleted all functions.php and header.php
    content and I pasted yours).

    your website is working with the other function;

    <ul class="menu">
    <li><a href="<?php echo home_url(); ?>/">Home</a></li>
    <?php wp_list_pages('title_li=' ); ?>
    </ul><!-- end .menu -->

    And I want to use in wp_nav_menu() and not in wp_list_pages() function.



    Forum Moderator

    In the absence of a custom menu and a custom callback, wp_nav_menu falls back to wp_page_menu – not wp_list_pages. In your case, it depends upon your display_pages() callback function.

    All right so if you have support for custom menus and if you don’t want to use container:

    <?php $defaults = array(
      'theme_location'  => ,
      'menu'            => ,
      'container'       => '',
      'container_class' => false,
      'container_id'    => ,
      'menu_class'      => 'menu',
      'menu_id'         => ,
      'echo'            => true,
      'fallback_cb'     => 'wp_page_menu',
      'before'          => ,
      'after'           => ,
      'link_before'     => ,
      'link_after'      => ,
      'depth'           => 0,
      'walker'          => );

    To remove container_class use (false – as shown above)
    To remove container use (” – and container is out)

    All this is working if theme supports custom menu and/or there are no other conflicts or issues.

    Refer to: http://codex.wordpress.org/Function_Reference/wp_nav_menu#Default_Usage

Viewing 15 replies - 1 through 15 (of 27 total)
  • The topic ‘Remove the div and ul tags from wp_nav_menu() function’ is closed to new replies.