navigation – Apply walker_nav_menu_start_el but only to one menu
Question
I’m building an accessible dropdown menu and successfully created a button to toggle the menu for screen readers that doesn’t interfere with a top-level link. I targeted the class menu-item-has-children
to do this, and thought I was applying it to only one menu, but it’s showing up in the footer where I don’t want or need it. Looking for a way to only apply this in wp_nav_menu
when a specific walker is applied.
// ADD SUBMENU BUTTON FOR A11Y
function test_menu_arrow($item_output, $item, $depth, $args) {
if (in_array('menu-item-has-children', $item->classes)) {
$aria_label = $item->title;
$aria_controls = $item->ID;
$arrow = '<button class="sub-menu-btn" aria-expanded="false" aria-controls="sub-menu-'.$aria_controls.'" aria-label="More '.$aria_label.'"></button>';
$item_output = str_replace('</a>', '</a>'. $arrow .'', $item_output);
}
return $item_output;
}
add_filter('walker_nav_menu_start_el', 'test_menu_arrow', 10, 4);
0
2 months
2022-12-01T15:44:24-05:00
2022-12-01T15:44:24-05:00 0 Answers
0 views
0
Leave an answer