Skip to content

Instantly share code, notes, and snippets.

@likesalmon
likesalmon / add_slug_class_to_menu_item.php
Created February 2, 2015 21:31
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));