Is there a good way to add a class of “parent” to list items that have children using wp_nav_menu?
I’ve found a couple of solutions online but non seem to have the desired effect.
Is there a good way to add a class of “parent” to list items that have children using wp_nav_menu?
I’ve found a couple of solutions online but non seem to have the desired effect.
You must be logged in to post a comment.
This may not be the ideal solution, depending on what you need the class for, but you could add a “parent” class via JavaScript. The line below uses the jQuery
:has()
selector:See WordPress codex http://codex.wordpress.org/Function_Reference/wp_nav_menu#How_to_add_a_parent_class_for_menu_item
Use this function:
You can do it with a bit of regex and a custom menu walker although I’ve not tested this thoroughly:
What this does is every time we start a new level within the menu we look at the last opening
<li>
in the string and add ‘has-submenu’ to the class attribute. Unfortunately at the time of generating the list item itself we don’t know if it has children or not.To use the above just pass in the walker argument where you declare your menu eg: