There is a mismatch of callback function names in your code. I believe, it’s just a typo.
To understand the nature of your problem, we have to know, how walker-nav-menu
uses its parameters, while traversing a menu tree. $args
apply to entire menu tree. $item
is an individual menu item.
Any changes to arguments in $args
object, made via filters, are persistent for every next iteration of walker-nav-menu
. Your code should be:
add_filter( 'nav_menu_item_args', 'nav_menu_modify_dropdown', 10, 2 );
function nav_menu_modify_dropdown( $args, $item ) {
unset( $args->link_after );
if ( $args->theme_location === 'primary-menu' && in_array( 'menu-item-has-children', $item->classes, true ) )
$args->link_after="<span></span>";
return $args;
}
If there is only one menu location, you can simplify your conditional statement:
if ( in_array( 'menu-item-has-children', $item->classes, true ) )
$args->link_after="<span></span>";