menus - Problem to asign class to <ul> with wp_nav_menu()

admin2025-06-06  6

I have some problems to asign a class to tag with wp_nav_menu() instead of asign the class to the tag it creates a and asign the class to that one.

My register_nav_menu function:

    function register_primary_menu() {
    register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'register_primary_menu');

wp_nav_menu:

<?php
                        $defaults = array(
                        'theme_location'  => 'primary-menu',
                        'menu'            => '',
                        'container'       => 'flase',
                        'container_class' => '',
                        'container_id'    => '',
                        'menu_class'      => 'nav navbar-nav',
                        'menu_id'         => '',
                        'echo'            => true,
                        'fallback_cb'     => 'wp_page_menu',
                        'before'          => '<li>',
                        'after'           => '</li>',
                        'link_before'     => '',
                        'link_after'      => '',
                        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
                        'depth'           => 0,
                        'walker'          => ''
                    );

                    wp_nav_menu( $defaults );

                    ?>

What I want is this:

<ul class="nav navbar-nav">
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Page 1</a></li>
                    <li><a href="#">Page 2</a></li> 
                    <li><a href="#">Page 3</a></li>
                    </ul>

And what I get is this:

<div class="nav navbar-nav">
<ul>
<li class="page_item page-item-56"><a href="[URL]">Page 1</a></li>
<li class="page_item page-item-58"><a href="[URL]">Page 2</a></li>
<li class="page_item page-item-60"><a href="[URL]">Page 3</a></li>
<li class="page_item page-item-2"><a href="[URL]">Sample Page</a></li>
</ul>
</div>

I have some problems to asign a class to tag with wp_nav_menu() instead of asign the class to the tag it creates a and asign the class to that one.

My register_nav_menu function:

    function register_primary_menu() {
    register_nav_menu('primary-menu', __('Primary Menu'));
}
add_action('init', 'register_primary_menu');

wp_nav_menu:

<?php
                        $defaults = array(
                        'theme_location'  => 'primary-menu',
                        'menu'            => '',
                        'container'       => 'flase',
                        'container_class' => '',
                        'container_id'    => '',
                        'menu_class'      => 'nav navbar-nav',
                        'menu_id'         => '',
                        'echo'            => true,
                        'fallback_cb'     => 'wp_page_menu',
                        'before'          => '<li>',
                        'after'           => '</li>',
                        'link_before'     => '',
                        'link_after'      => '',
                        'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
                        'depth'           => 0,
                        'walker'          => ''
                    );

                    wp_nav_menu( $defaults );

                    ?>

What I want is this:

<ul class="nav navbar-nav">
                    <li><a href="#">Home</a></li>
                    <li><a href="#">Page 1</a></li>
                    <li><a href="#">Page 2</a></li> 
                    <li><a href="#">Page 3</a></li>
                    </ul>

And what I get is this:

<div class="nav navbar-nav">
<ul>
<li class="page_item page-item-56"><a href="[URL]">Page 1</a></li>
<li class="page_item page-item-58"><a href="[URL]">Page 2</a></li>
<li class="page_item page-item-60"><a href="[URL]">Page 3</a></li>
<li class="page_item page-item-2"><a href="[URL]">Sample Page</a></li>
</ul>
</div>
Share Improve this question asked Aug 20, 2015 at 8:29 AlexAlex 3757 silver badges18 bronze badges 2
  • I noticed 'container'=> 'flase', here 'flase' instead of false, this must not be a cause, but check correcting it. – Domain Commented Aug 20, 2015 at 8:41
  • Fixed that mispelling :) But my issue still remains :) – Alex Commented Aug 20, 2015 at 10:10
Add a comment  | 

3 Answers 3

Reset to default -2

Make sure your menu does setup correctly in that location in Appearance / Menus, unless it wont display that class. After you set the theme location to Primary Menu, it should work fine. No idea why, guess its a bug in WordPress.

Also fix the typo: 'container' => false

You need to set 'container' => 'ul'.

I have same problem and i fix it. My theme_location i set primary in the functions.php file, but i don't set that location in the wp admin -> Appearance and in the Menu Settings set your location.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749192970a317140.html

最新回复(0)