Back to Top

How to change SubMenu Class using Walker in WordPress

To change SubMenu Class using Walker in WordPress

WordPress has the wp_nav_menu() function which is quite powerful and can take a number of parameters that will allow to control the classes and IDs of html tags, the name of the menu and many more.

In this article, we will check modify submenu class name using the walker in wp_nav_menu. Wp_nav_menu is a WordPress function which is useful to display menus to the frontend.

This function provides many parameters using that you can modify display part of the menu. But it is not enough for a satisfying requirement. Sometimes we want to change the class of inner html section which is not possible using provided parameters so WordPress provided another option is known as Walker Class. You can extend Walker_Nav_Menu class and do modification you want to.

Also Read: To change File/Image Upload Path in WordPress

The walker I’d like to point out here is the wp_nav_menu() function, which looks like this:

                  'menu'     => 'Main Menu', 
                  'walker'   => new CD_Walker_Nav_Menu()

// Change Sub-Menu class name
class CD_Walker_Nav_Menu extends Walker_Nav_Menu {
	function start_lvl( &$output, $depth = 0, $arg = Array() ) {
		$indent = str_repeat( "\t", $depth );
		$output .= "\n$indent
\n" . __('back', 'cd') . "
    "; } function end_lvl( &$output, $depth = 0, $arg = Array() ) { $indent = str_repeat( "\t", $depth ); $output .= "\n$indent
"; } }


Here we have extended the Walker_Nav_Menu class with the new class and in that want to change submenu class name so used two functions start_lvl and end_lvl which will check the depth of menu and replace the class name.

That’s all there is to it! Hope this solution will solve your problem and helpful to you.Stay tuned with creativedev for more information 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Really! PHP touches the 200 Million Mark

Posted on 6 years ago

John Pitt

How to display XML content with PHP

Posted on 6 years ago


Learn about the Docker and how it works

Posted on 2 years ago


In depth Understanding of Laravel Core

Posted on 3 weeks ago