Removing container from wp_nav_menu not working

I’m converting this html menu to wordpress:

<ul>
    <li><a href="/" class="current"><span>Home</span></a></li>
    <li><a href="/"><span>About</span></a></li>
</ul>

i use:

Read More
wp_nav_menu(array(
 'menu'=>'mainmenu' ,
 'container'       => false, 
 'link_before'     => '<span>',
 'link_after'      => '</span>',
 'theme_location'  => 'primary')
 );

but the html i get is:

<div class="menu">
    <ul>
        <li class="current_page_item"><a href="http://localhost/goodsoil/" title="Home"><span>Home</span></a></li>
        <li class="page_item page-item-2"><a href="http://localhost/goodsoil/?page_id=2" title="About"><span>About</span></a></li>
    </ul>
</div>

If i use a custom menu then i get:

<ul id="menu-test" class="menu">
    <li id="menu-item-6" class="menu-item menu-item-type-custom current-menu-item current_page_item menu-item-home menu-item-6"><a href="url/"><span>Home</span></a></li>
    <li id="menu-item-5" class="menu-item menu-item-type-post_type menu-item-5"><a href="url/?page_id=2"><span>About</span></a></li>
</ul>

but if i don’t use a custom menu ‘container’=>false doesn’t work
Any solution?

Related posts

Leave a Reply

6 comments

  1. [SOLVED]
    IT DOES NOT WORK when you are referring to an inexisting location. e.g. when you copied the code from somewhere else, or you haven’t created your menu or location yet in the dasboard.

    e.g. remove “, ‘theme_location’ => ‘primary'” from the following code:

        wp_nav_menu( array( 'container'=> false, 'menu_class'=> false, 'menu_id'=> 'ia_toplevel', 'theme_location' => 'primary' ) );
    

    so it shoult look like

        wp_nav_menu( array( 'container'=> false, 'menu_class'=> false, 'menu_id'=> 'ia_toplevel' ) );
    

    It works fine WITHOUT container in my website SocialBlogsiteWebDesign.com

  2. Found the solution.

    Simply use “ul” for ‘container’

    wp_nav_menu(array(
     'menu'=>'mainmenu' ,
     'container' => 'ul',
     'menu_class'=> '',
     'link_before'     => '<span>',
     'link_after'      => '</span>',
     'theme_location'  => 'primary')
     );
    
  3. you are referring to the fallback function of the nav_menus. This is “wp_page_menu” and unfortunatelly it doesn’t produce the same type of markup that a custom menu does.

    To fix this create your own fallback menu. I’m using this:

    function my_page_menu($args){
      $menu = '';
      $args['echo'] = false;
      $args['title_li'] = '';
    
      // If the front page is a page, add it to the exclude list
      if (get_option('show_on_front') == 'page') $args['exclude'] = get_option('page_on_front');
    
      $menu .= str_replace(array("r", "n", "t"), '', wp_list_pages($args));
    
      if($menu):
        if($args['container']) $menu = '<'.$args['container'].' class="'.$args['container_class'].'">'.$menu;
        $menu .= '<ul class="'.$args['menu_class'].'">'.$menu;
    
        // add 'home' menu item
        $menu .= '<li class="home '.((is_front_page() && !is_paged()) ? 'current-menu-item' : null).'"><a href="'.home_url('/').'" title="'.__("Home Page").'">'.$args['link_before'].__("Home").$args['link_after'].'</a></li>';
    
        $menu .= '</ul>';
        if($args['container']) $menu .= '</'.$args['container'].'>n"';
        $menu = apply_filters('wp_page_menu', $menu, $args);
      endif;
    
      echo $menu;
    }
    

    You might need to change this as my theme handles menus its own way, but I think you get the basic idea.

    And when you’re calling wp_nav_menu do it like wp_nav_menu(array('fallback_cb' => 'my_page_menu'));

    You can also match the classes to the ones of a custom menu using this:

    add_filter('page_css_class', 'atom_page_css_classes', 420, 2);
    function atom_page_css_classes($classes, $page){
      // overwrite and use page (safe) name instead of ID; nobody styles IDs...
      $new_classes = array('page-'.$page->post_name);
    
      // adjust active menu classes to match the ones added by wp_nav_menu()
      foreach($classes as $class)
        if($class == 'current_page_item') $new_classes[] = 'current-menu-item';
        elseif($class == 'current_page_parent') $new_classes[] = 'current-menu-parent';
        elseif($class == 'current_page_ancestor') $new_classes[] = 'current-menu-ancestor';
    
      return $new_classes;
    }
    

    This way you cut down a few CSS rules…

  4. I developed a simple and efficient solution using str_replace, which can be applied separately for use in menu tables rather than ul li:

    $menu = wp_nav_menu( array( 
        'theme_location' => 'main-menu', 
        'container_id' => 'mainMenu', 
        'container_class' => 'ddsmoothmenu',
        'echo' => FALSE
    ));
    
    $menu = str_replace(
        array('ul id="main-menu" class="menu">','/ul>','li','/li>'), 
        array('table id="main-menu" class="menu">tr>','/tr>/table>','td','/td>'), 
        $menu
    );
    
    echo $menu;