[Resolved] Add data attribute tag to menu link

Home Forums Support [Resolved] Add data attribute tag to menu link

Home Forums Support Add data attribute tag to menu link

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #976313
    David

    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

    #976325
    David
    Staff
    Customer Support

    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 );
    #976331
    David

    Perfect – thanks David!

    #976334
    David
    Staff
    Customer Support

    You’re welcome

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