Categories
Code

Add menu li class of item’s name in WordPress

This adds a class of the item’s name to each list item in a nav menu in WordPress. So, if the page item is called ‘About us’, the li gets an extra class of ‘menu-item-about-us’. Handy for styling.

add_filter('nav_menu_css_class' , 'ed_page_title_class' , 10 , 2);
function ed_page_title_class($classes, $item){
  $classes[] = sanitize_title('menu-item-' . $item->title);
  return $classes;
}