Skip to content

Instantly share code, notes, and snippets.

@mckernanin
Created December 30, 2015 21:37
Show Gist options
  • Save mckernanin/442603ff720de428ff45 to your computer and use it in GitHub Desktop.
Save mckernanin/442603ff720de428ff45 to your computer and use it in GitHub Desktop.
Menu Slug as Class - WordPress Navigation
<?php
//Add Slug to Menu as Class
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