Blog

Adding an attribute and value to a WordPress Menu Item

i have add some data value in menu for filter the value in post grid and find that  There is no facility to add this to a Menu Item in a menu in WP Dashboard, however their are 2 solutions on how this can be done.

Their is a filter available to add this nav_menu_link_attributes

add_filter( 'nav_menu_link_attributes', 'filter_function_name', 10, 3 );

function filter_function_name( $atts, $item, $args ) {
    // Manipulate attributes
    return $atts;
}

In the Dashboard add in a Custom Link give it the modal link like href=”#MyModal”

Get the ID in the code using Web Inspector tools, add the ID as a variable, once that ID is matched add in the new attribute and value, in the case below data-filter and  other data

add_filter( 'nav_menu_link_attributes', 'themeprefix_menu_attribute_add', 10, 3 );
function themeprefix_menu_attribute_add( $atts, $item, $args )
{
  
  if ($item->ID == $menu_link) {
    $atts['data-filter'] = 'home'; //  you can add there filter category name here 
  }
  //Return the new attribute
  return $atts;
}

 

Leave a Reply