Home > OS >  How do I add class of dropdown to wp_nav_menu() function
How do I add class of dropdown to wp_nav_menu() function

Time:11-02

How do I add class of dropdown in a class wrap by li - <li > in a wp_nav_menu function?

My static menu

<ul>
<li>
<a href="#hero">Home</a></li>
      <li><a href="blog.html">Blog</a></li>
      <li ><a href="#"><span>Drop Down</span> <i ></i></a>
        <ul>
          <li><a href="#">Drop Down 1</a></li>
          <li ><a href="#"><span>Deep Drop Down</span> <i ></i></a>
            <ul>
              <li><a href="#">Deep Drop Down 1</a></li>
            </ul>
          </li>
          <li><a href="#">Drop Down 2</a></li>
        </ul>
      </li>
</ul>

I have this dynamic nav menu function,

<?php
if ( has_nav_menu( 'main-menu' ) ) :
    wp_nav_menu( array(
        'theme_location' => 'main-menu',
        'items_wrap'     => '%3$s',
        'add_li_class'   => '',
        'container' => ''
    ));
endif;
?>

How do I make my static menu dynamic with a dropdown in wordpress wp_nav_menu function?

CodePudding user response:

You can use this

if ( has_nav_menu( 'main-menu' ) ) :
    wp_nav_menu( array(
        'theme_location' => 'main-menu',
        'items_wrap'     => '%3$s',
        'menu_class'   => 'menu_class_name', // (string) CSS class to use for the ul element which forms the menu. Default 'menu'. and you can use .menu_class_name li {} for css
        'container' => '' 
    ));
endif;

CodePudding user response:

You have 2 options.

You can add the custom class in the wordpress backend. https://sevenspark.com/how-to/how-to-add-a-custom-class-to-a-wordpress-menu-item

Or you can use this custom php code in your functions.php

function add_menu_item_class( $classes, $item ) {       
    $classes[] = YOURCLASSNAME;
    return $classes;
} 

add_filter( 'nav_menu_css_class' , 'add_menu_item_class' , 10, 2 );
  • Related