Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wp_nav_menu change sub-menu class name?

Tags:

wordpress

Is there a way to change the child <ul class="sub-menu"> generated by WordPress itself to a custom class name?

I know the parent <ul> you can remove or change the name with 'menu_class' => 'newname'.

I couldn't find the answer. Itried 'submenu_class' => 'customname'. It seems logic to me, but obviously that is no the right one.

any ideas?

like image 890
Cam Avatar asked Feb 17 '11 21:02

Cam


People also ask

How can the class name of a sub menu be changed in the Wp_nav_menu?

You can just change it in wordpress file. location : www/project_name/wp-includes/nav-menu-template. php. open this file and at line number 49, change the name of sub-menu class with your custom class.

What is the Wp_nav_menu () function used for?

Usage. wp_nav_menu( $args ); Given a theme_location parameter, the function displays the menu assigned to that location. If no such location exists or no menu is assigned to it, the parameter fallback_cb will determine what is displayed.

How do you call the header menu in WordPress?

To add a custom navigation menu, the first thing you need to do is register your new navigation menu by adding this code to your theme's functions. php file. add_action( 'init' , 'wpb_custom_new_menu' ); You can now go to Appearance » Menus page in your WordPress admin and try to create or edit a new menu.


2 Answers

There is no option for this, but you can extend the 'walker' object that WordPress uses to create the menu HTML. Only one method needs to be overridden:

class My_Walker_Nav_Menu extends Walker_Nav_Menu {   function start_lvl(&$output, $depth) {     $indent = str_repeat("\t", $depth);     $output .= "\n$indent<ul class=\"my-sub-menu\">\n";   } } 

Then you just pass an instance of your walker as an argument to wp_nav_menu like so:

'walker' => new My_Walker_Nav_Menu() 
like image 166
Richard M Avatar answered Sep 20 '22 12:09

Richard M


This is an old question and I'm not sure if the solution I'm going to mention was available by the time you asked, but I think it's worth mentioning. You can achieve what you want by adding a filter to nav_menu_submenu_css_class. See the example below - you can replace my-new-submenu-class by the class(es) you want:

function my_nav_menu_submenu_css_class( $classes ) {     $classes[] = 'my-new-submenu-class';     return $classes; } add_filter( 'nav_menu_submenu_css_class', 'my_nav_menu_submenu_css_class' ); 
like image 21
FlavioEscobar Avatar answered Sep 19 '22 12:09

FlavioEscobar