Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question
    
What do you mean by if i don't use a custom menu ? Could you please describe more clearly under which conditions you get desired result and when not? –  Rarst Nov 8 '10 at 20:33
    
My theme supports custom menus. If i use a custom menu the 'container'=>false works fine, but if not it doesn't works. If i use 'container'=>'' happens the same. –  Oterox Nov 9 '10 at 15:29
add comment

5 Answers

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;
share|improve this answer
add comment

Try This

<?php wp_nav_menu( array( 'menu' => 'Menu','link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
share|improve this answer
add comment

[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

share|improve this answer
    
its works fine ! thanks ! –  papachan Dec 1 '11 at 12:12
add comment

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...

share|improve this answer
add comment
Try: <?php wp_nav_menu( array( 'container' => '' ) ); ?>

http://codex.wordpress.org/Function_Reference/wp_nav_menu

share|improve this answer
1  
It is checked as if ( $args->container ) in code so false or empty string doesn't make a difference. –  Rarst Nov 8 '10 at 20:34
    
Rarst, I just tested it with a site I've been working on lately, and setting 'container' => '' did actually work. I think it's not testing for value, but more setting to null and thus not outputting data. –  tw2113 Nov 8 '10 at 22:21
    
'container' => '' doesn't work for me. I'm using wp 3.0.1 –  Oterox Nov 9 '10 at 15:25
    
doesnt work for me too ... with wp 3.2.1 –  papachan Dec 1 '11 at 12:11
add comment

protected by toscho Jun 13 '12 at 5:58

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.