Created
December 30, 2015 21:37
-
-
Save mckernanin/442603ff720de428ff45 to your computer and use it in GitHub Desktop.
Menu Slug as Class - WordPress Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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