1. Home
  2. Wordpress
  3. Add slug class to menu item
#wordpress#php#menu

Use this filter to add a slug-based css class to each menu item.

function add_menu_item_slug_class($classes, $item, $args) {

	$item_class = sanitize_title($item->title); // Convert title to a slug
	$classes[] = "menu-item-" . $item_class;

	return $classes;

}
add_filter('nav_menu_css_class', 'add_menu_item_slug_class', 1, 3);
copy
Full Wordpress cheatsheet
Sitemap Git repository