How do I give class to the dropdown sub-menu in the wp_nav_menu?

You can use Walker_Nav_Menu (WordPress Default class).

Here is an example –

In header.php file –

<nav id="header-menu">
<?php
  wp_nav_menu( array(
    'theme_location' => 'primary',
    'walker'  => new Child_Wrap()   
   ) );
?>
</nav>

In my functions.php file –

class Child_Wrap extends Walker_Nav_Menu
{
    function start_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "\n<div class="sub-menu-holder">$indent<ul class=\"drop-down\">\n";
    }

    function end_lvl(&$output, $depth = 0, $args = array())
    {
        $indent = str_repeat("\t", $depth);
        $output .= "$indent</ul></div>\n";
    }
}

Above is an example of my code you can change as per your requirement.
Thanks.

Leave a Comment