Archived topic
Add data attribute tag to menu link
3 replies · Started by David on August 5, 2019
Hello
I'm trying to get the contact link in my menu to open up a pop window but it requires data-rel="lightcase:myCollection" to be added to the A link.
I've tried to follow this to get it to add something similar...
https://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes
I've set up a hook to appear in wp-footer with the below script but can't seem to get it to work...
<script>
function add_specific_menu_location_atts( $atts, $item, $args ) {
// check if the item is in the primary menu
if( $args->theme_location == 'primary' ) {
// add the desired attributes:
$atts['data-rel'] = 'lightcase:myCollection';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 45 );
</script>
Any ideas what I'm doing wrong?
Link is... http://185.20.51.60/~pjdservicesco/
Thanks
Dave
Hi there,
try this filter:
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 40 === $item->ID ) { // change 40 to the ID of your menu item.
$atts['data-rel'] = 'lightcase:myCollection';
}
return $atts;
}, 10, 3 );
Perfect - thanks David!
You're welcome