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?
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:
Then you just pass an instance of your walker as an argument to
wp_nav_menu
like so: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 replacemy-new-submenu-class
by the class(es) you want:Choice of options.
It remains to include the file with an custom walker and point it to the menu:
You can use WordPress preg_replace filter (in your theme functions.php file)
example:
Here’s an update to what Richard did that adds a “depth” indicator. The output is level-0, level-1, level-2, etc.
You don’t need to extend the Walker. This will do:
Like it always is, after having looked for a long time before writing something to the site, just a minute after I posted here I found my solution.
It thought I’d share it here so someone else can find it.
This is where I found the solution:
Solution in WordPress support forum
I had to change:
function start_lvl(&$output, $depth)
to:
function start_lvl( &$output, $depth = 0, $args = array() )
Because I was getting an incompatibility error:
Strict Standards: Declaration of My_Walker_Nav_Menu::start_lvl() should be compatible with Walker_Nav_Menu::start_lvl(&$output, $depth = 0, $args = Array)
This may be useful to you
How to add a parent class for menu item
Adding Conditional Classes to Menu Items
For reference : click me
You can just use a Hook
where
in the above i need a small change which i am trying to place but i am not able to do that,
your output will look like this
what i am looking for
in the above one i have placed the parent class inside the parent anchor link that
<li id="menu-item-13" class="depth0"><a class="parent" href="#">About Us</a>
I would suggest to replace your cutomclass css class name to sub-menu.
use find and replace: ie. find: .customclass replace with .sub-menu,
works for me.
To change the default “sub-menu” class name, there is simple way.
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.
Or you can also add your custom class next to sub-menu.
Done.
It worked for me.I used wordpress-4.4.1.