Reputation: 33
I'm working on a Wordpress website that has multiple menus set up. I wanted to add custom attributes to the menu items in one of the menus using 'nav_menu_link_attributes', but that adds the attributes to all of the menus. How would I limit this to just one specific menu? I can't find any documentation on this question.
My current code is:
function add_menu_atts($atts){
$atts['data-inventory-link'] = $atts['href'];
$atts['data-model'] = $atts['title'];
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_menu_atts');
Upvotes: 1
Views: 4705
Reputation: 275
The filter you are using nav_menu_link_attributes
supports other arguments as well. You can pass in a 2nd parameter $item
and a 3rd parameter $args
which contains the item details. Try something like this:
function add_menu_atts($atts, $item, $args){
// your check for primary menu location
if( $args->theme_location == 'primary' ) {
$atts['data-inventory-link'] = $atts['href'];
$atts['data-model'] = $atts['title'];
}
return $atts;
}
add_filter('nav_menu_link_attributes', 'add_menu_atts', 10, 3);
Upvotes: 7