Archived topic
How to add an ID (#) to a menu item
1 reply · Started by Ricardo on December 23, 2022
Hi,
I have a script that is triggered when someone clicks in an area with a specific ID. For example:
- I add "tiqets" to block/advanced/html anchor;
- I add this to wp_footer:
<script
src="https://tiqets-cdn.s3.amazonaws.com/booking_engine/loader/11384.js"></script>
- Someone clicks on the area of that block with #tiqets and it triggers the above;
I've tried with css classes, using .tiqets instead of #tiqets on the trigger-selector, and adding "tiqets" to blocl/advanced/additional css classes, but it didn't work.
I would like to trigger this when someone clicks on a specific menu item in the primary navigation and also in the off canvas menu. For this I would need tho add an ID to a menu item. Is this possible? If not is there any turnaround?
Thank you for your help
Hi there,
there is the nav_menu_link_attributes filter hook:
https://developer.wordpress.org/reference/hooks/nav_menu_link_attributes/
And here is PHP snippet that adds an ID to a menu item based on the menu items ID.
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 40 === $item->ID ) { // change 40 to the ID of your menu item.
$atts['id'] = 'tiqets';
}
return $atts;
}, 10, 3 );
Replace the 40 with the ID of the menu item.
