Site logo

[Support request] How to add an ID (#) to a menu item

Home Forums Support [Support request] How to add an ID (#) to a menu item

Home Forums Support How to add an ID (#) to a menu item

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2471433
    Ricardo

    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&gt;

    – 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

    #2471762
    David
    Staff
    Customer Support

    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.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.