Skip to content

Instantly share code, notes, and snippets.

@sewmyheadon
Created October 21, 2016 19:12
Show Gist options
  • Save sewmyheadon/de6805d14367264bfdaf910f54c14cee to your computer and use it in GitHub Desktop.
Save sewmyheadon/de6805d14367264bfdaf910f54c14cee to your computer and use it in GitHub Desktop.
Add class to specific WordPress navigation menu link
/**
* Add class to specific WordPress navigation menu link
* Reference: https://codex.wordpress.org/Plugin_API/Filter_Reference/nav_menu_link_attributes
*/
add_filter('nav_menu_link_attributes', 'ivycat_account_secondary_menu_link_attributes', 10, 4);
function ivycat_account_secondary_menu_link_attributes($atts, $item, $args, $depth){
if ($args->menu->slug == 'utility' && $item->ID == 26){
$class = "account_button";
// Make sure not to overwrite any existing classes
$atts['class'] = (!empty($atts['class'])) ? $atts['class'].' '.$class : $class;
}
return $atts;
}
@sewmyheadon
Copy link
Author

Notes:
In this example:

  1. utility is the slug for the custom menu
  2. 26 is the ID of that specific menu item
  3. account_button is the class or classes that you want to add to the link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment