Skip to content

Instantly share code, notes, and snippets.

@jackreichert
Created July 25, 2013 23:53
Show Gist options
  • Save jackreichert/6084851 to your computer and use it in GitHub Desktop.
Save jackreichert/6084851 to your computer and use it in GitHub Desktop.
This is how you add a class to menu items in WordPress. Change the "theme_location" to == "primary" to any menu name you like. Or remove the condition to have it applied everywhere.
add_filter( 'wp_nav_menu_objects', 'add_menu_class', 10, 2 );
function add_menu_class( $sorted_menu_items, $args ) {
if ($args->theme_location == "primary")
foreach ($sorted_menu_items as $i => $menu )
$sorted_menu_items[$i]->classes[] = 'color-'.strtolower(str_replace(' ', '-', $menu->post_title));
return $sorted_menu_items;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment