开发者

Remove <div> wrapper from wp_nav_menu output

I'm trying to remove the wrapper from the wp_nav_menu() function.

I've passed container => false to the arguements array and added a hook in my functions.php, but it still shows the wrapper.

function my_wp_nav_menu_args( $args )
{
    $args['menu'] = false;
    $args['menu_class'] = false;
    $args['container'] = false;
    $args['container_class'] = false;
    $args['show_home'] = true;

    return $args;
}
开发者_开发百科

Any ideas why?


Reading the codex: Function Reference/wp nav menu

You may need to set the Theme Location in your functions.php file and then assign your menu to it?

This is what is says in the codex:

In order to remove navigation container, theme location specified in functions.php and used among arguments in function wp_nav_menu ( eg. 'theme_location' => 'primary-menu' ) must have a menu assigned to it in administration! Othervise argument 'container' => 'false' is ignored.

If you need to register a location you can use the following:

// This theme uses wp_nav_menu() in one location.
register_nav_menus( array(
    'primary' => __( 'Primary Navigation', 'Your_Theme' ),
) );

Then pass it in the wp_nav_menu() function

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

Hope this helps!


I have passed null to container and menu is not wrapped in div tag.

wp_nav_menu(
    array('menu' => 'Bočni',
        'container' => null
    )
);


I think the boolean data type it's not available for this Parameters, please read this Function Reference/wp nav menu

So the best way to remove this div wrapper replace it with ul, like this example

wp_nav_menu( array(
  'menu' => 'header-nav_menu',
  'theme_location'    => 'header-nav_menu',
  'container'         => 'ul', //To replace div wrapper with ul 
  'menu_class'        => 'YOUR CLASS'//Add classes to your ul
 )
);


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

An existing menu have to be associated to your location ('footer-1' in my example), otherwise, there is always a div wrapper.


In my case "container" => false doesn't work, It only works when you write
"items_wrap" => "%3$s" but it removes all wraps including <ul> if you want to remove <div> and have your menu wrapped in <ul> I recommend to do this way

wp_nav_menu(array(
    'theme_location'  => 'header_menu',
    'menu'            => 'header_menu',
    'items_wrap'      => '<ul>%3$s</ul>',
    'container'       => false,
    'menu_class'      => 'nav',
    'list_item_class' => 'nav-item',
    'link_class'      => 'nav-link',
    'menu_id' => 'menu-main-menu'
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜