Wordpress - How to add a data attribute to a WordPress menu item
Specifically editing the code you provided in the original question:
add_filter( 'nav_menu_link_attributes', 'wpse121123_contact_menu_atts', 10, 3 );
function wpse121123_contact_menu_atts( $atts, $item, $args )
{
// The ID of the target menu item
$menu_target = 123;
// inspect $item
if ($item->ID == $menu_target) {
$atts['data-toggle'] = 'modal';
}
return $atts;
}
The second $item
argument, which is being made available to your filter function, contains a menu item object. If dumped it looks something like this:
[1] => WP_Post Object
(
[ID] => 2220
[post_author] => 1
[post_date] => 2012-12-26 19:29:44
[post_date_gmt] => 2012-12-26 17:29:44
[post_content] =>
[post_title] => Home
[post_excerpt] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => home-3
[to_ping] =>
[pinged] =>
[post_modified] => 2013-06-05 01:55:20
[post_modified_gmt] => 2013-06-04 22:55:20
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://dev.rarst.net/?p=2220
[menu_order] => 1
[post_type] => nav_menu_item
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
[db_id] => 2220
[menu_item_parent] => 0
[object_id] => 2220
[object] => custom
[type] => custom
[type_label] => Custom
[title] => Home
[url] => http://dev.rarst.net/
[target] =>
[attr_title] =>
[description] =>
[classes] => Array
(
[0] =>
[1] => menu-item
[2] => menu-item-type-custom
[3] => menu-item-object-custom
[4] => current-menu-item
[5] => current_page_item
[6] => menu-item-home
)
[xfn] =>
[current] => 1
[current_item_ancestor] =>
[current_item_parent] =>
)
To target specific menu item you need to formulate your condition and check it against data available in the object, for example if ( 2220 == $item['ID'] )