Advertisement
leadbellydesign

Add page/post slug class to menu item classes

Dec 8th, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. //http://www.wpreso.com/blog/tutorials/2011/01/04/add-pagepost-slug-class-to-menu-item-classes/
  2. function add_slug_class_to_menu_item($output){
  3.     $ps = get_option('permalink_structure');
  4.     if(!empty($ps)){
  5.         $idstr = preg_match_all('/<li id="menu-item-(\d+)/', $output, $matches);
  6.         foreach($matches[1] as $mid){
  7.             $id = get_post_meta($mid, '_menu_item_object_id', true);
  8.             $slug = basename(get_permalink($id));
  9.             $output = preg_replace('/menu-item-'.$mid.'">/', 'menu-item-'.$mid.' menu-item-'.$slug.'">', $output, 1);
  10.         }
  11.     }
  12.     return $output;
  13. }
  14. add_filter('wp_nav_menu', 'add_slug_class_to_menu_item');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement