Skip to content

Instantly share code, notes, and snippets.

@likesalmon
Created February 2, 2015 21:31
Show Gist options
  • Save likesalmon/f1f1184fdabd6931aaf5 to your computer and use it in GitHub Desktop.
Save likesalmon/f1f1184fdabd6931aaf5 to your computer and use it in GitHub Desktop.
Add slug to css classes in wp_nav_menu() menu items. For use in /wp-content/themes/myTheme/functions.php.
/**
* Add slugs to custom menu items
*/
function add_slug_class_to_menu_item($output){
$ps = get_option('permalink_structure');
if(!empty($ps)){
$idstr = preg_match_all('/<li id="menu-item-(\d+)/', $output, $matches);
foreach($matches[1] as $mid){
$id = get_post_meta($mid, '_menu_item_object_id', true);
$slug = basename(get_permalink($id));
$output = preg_replace('/menu-item-'.$mid.'">/', 'menu-item-'.$mid.' menu-item-'.$slug.'">', $output, 1);
}
}
return $output;
}
add_filter('wp_nav_menu', 'add_slug_class_to_menu_item');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment