0

I am writing below code.

$values = array(
       'theme_location'  => 'primary',
       'menu'            => 'efgh',
       'container'       => 'div',
       'container_class' => 'xyz',
       'container_id'    => 'pqr',
       'menu_class'      => 'menu_123',
       'menu_id'         => 'mno',
       'items_wrap'      => '<ul id="mouse_123" class="rat_456">Laptop</ul>'
);

wp_nav_menu( $values );

And getting below out put.

<nav>
    <div class="menu_123">
      <ul>
          <li class="page_item page-item-27"><a href="http://localhost/wordpress/?page_id=27">Bucket</a></li>
          <li class="page_item page-item-2"><a href="http://localhost/wordpress/?page_id=2">Jug</a></li>
          <li class="page_item page-item-19"><a href="http://localhost/wordpress/?page_id=19">Cup</a></li>
     </ul>
   </div>
</nav>

I would like to get output like below.

  <nav>
    <div class="menu_123">
      <ul class="my_class" id="my_id">
          <li class="page_item page-item-27"><a href="http://localhost/wordpress/?page_id=27">Bucket</a></li>
          <li class="page_item page-item-2"><a href="http://localhost/wordpress/?page_id=2">Jug</a></li>
          <li class="page_item page-item-19"><a href="http://localhost/wordpress/?page_id=19">Cup</a></li>
     </ul>
   </div>
</nav>

Thanks @vlood. I am trying a lot but I could not push any class or id to ul. I am updating my post.

First UPDATE

In my fucntion.php file

register_nav_menu( 'primary-menu', __( 'Primary Menu' ) );

In my header.php file

<nav>
 <?php 
   wp_nav_menu( 
     array(
       'container' => false,
       'theme_location' => 'primary-menu',
       'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>'
     )); 

 ?>
</nav>

I am getting output like below.

<nav>
      <div class="menu">
        <ul>
           <li class="page_item page-item-27">
              <a href="http://localhost/wordpress/?page_id=27">Bucket</a>
          </li>
          <li class="page_item page-item-2">
              <a href="http://localhost/wordpress/?page_id=2">Mug</a>
          </li>
          <li class="page_item page-item-19">
              <a href="http://localhost/wordpress/?page_id=19">Jug</a>
         </li>
      </ul>
    </div>
</nav>

I tried several ways but could not solve the problem

Second UPDATE

I found an interesting issue in Twenty Fourteen Theme

In header.php of Twenty Fourteen Theme

                   `<?php 
                        wp_nav_menu( array( 'theme_location' => 'primary') ); 
                    ?>`

In functions.php of Twenty Fourteen Theme

// This theme uses wp_nav_menu() in two locations. register_nav_menus( array( 'primary' => __( 'Top primary menu', 'twentyfourteen' ), 'secondary' => __( 'Secondary menu in left sidebar', 'twentyfourteen' ), ) );

Output of Twenty Fourteen Theme.

<nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation"> <div class="menu-mouse-container"> <ul id="menu-mouse" class="menu"> <li id="menu-item-26" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-26"> <a href="http://localhost/wordpress/">Home</a> </li> <li id="menu-item-25" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-25"> <a href="http://localhost/wordpress/?page_id=19">Mouse</a> </li> </ul> </div>
</nav>

How is it possible to place id like menu-mouse and class like menu in ul ??

0

For the ul part you should use this:

'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',

%3$s is a placeholder for the list items, so you shouldn't change it to "Laptop" as you have done.

These are the parameters for the id and the class, that need to go together with items_wrap:

'menu_class'      => 'menu_123',
'menu_id'         => 'mno',

Why it works like this? Answer is here:

Remove wrapping div and ul from output of wp_nav_menu

  • Thanks @vlood. But your solution is not working. – Foysal Dec 26 '14 at 21:43
  • My bad, try with %3$s instead of %1$s :) – vlood Dec 26 '14 at 22:05
  • Thanks @vlood. This time I tried. $values = array( 'theme_location' => 'primary', 'items_wrap' => '<ul id="my_id" class="my_class">%3$s</ul>' ); wp_nav_menu( $values ); But result was as previous. So far I could not push any class or id in ul. Thanks – Foysal Dec 26 '14 at 22:42
  • Damn, it's my fault again. I tested it and it works when you use it like this: 'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>' Then it will get menu_id and menu_class in the %1$s and %2$s placeholders. I'm now updating the original answer with this. – vlood Dec 27 '14 at 9:28
  • Okay, I did according updates in the answer - you need to define these, if you want them in the ul: 'menu_class' and 'menu_id' – vlood Dec 28 '14 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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