I found examples adding a class to top level items, so we can display an arrow in menu items with sub-items, but is seems terrible to cope with the already built in WordPress classes, can’t display the arrow with current and CSS hover, it just ruins all states.
The current nav menu is like this <li><a>Text</a></li>
Is there someway to add a <span class="arrow"></span>
within the parent <a></a>
tags instead?!
Add -> “<span class="arrow"></span>
” -> inside <a/></a>
tags
Thus -> <li><a>Text<span class="arrow"></span></li></a>
that is parent.
the current code Adds the <span></span>
tags outside the <a></a>
tags
class My_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("t", $depth);
if('primary' == $args->theme_location && $depth ==0){
$output .='<span class="arrow"></span>';
}
$output .= "n$indent<ul class="sub-menu">n";
}
}
You are overwriting the incorrect method. You need the
start_el
instead. Here is the code for it:This code will add a
<span class="sub-arrow"></span>
to top-level menu items from the menu selected for theprimary
theme location in case that this menu item has any child items.