How to use class on primary menu ul instead of div in wordpress

408 views Asked by At

I used this php coding in wordpress to get primary menu.

<?php

  wp_nav_menu( array(
    'menu' => 'Top menu',
    'theme_location' => 'primary',
    'depth' => 2,
    'menu_class' => 'nav navbar-nav',
  ));

?>

This is the output in preview.

<div class="nav navbar-nav">
  <ul>
    <li class="active"><a href="#"> NEWS  <span class="sr-only">(current)</span></a></li>
    <li><a href="#">FUNNY PICTURES</a></li>
    <li><a href="#">FUNNY VIDEOS</a></li>
    <li><a href="#">GOVERMENT JOBS</a></li>
    <li><a href="#">HEALTH</a></li>
    <li><a href="#">BEST SHOPING DEAL</a></li>
  </ul>
</div>

This should work like below, not like above. Please explain what the change is need to made in php code.

<div>
  <ul class="nav navbar-nav">
    <li class="active"><a href="#"> NEWS  <span class="sr-only">(current)</span></a></li>
    <li><a href="#">test</a></li>
  </ul>
</div>
1

There are 1 answers

0
shrestha rohit On

Look at the wordpress documentation which states clearly all the things you need .

<?php

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

wp_nav_menu( $defaults );

?>

i didn't clearly understand your question but from what i have understood you may use container class to add class to the div folder and menu_class add class name to ul element .