Skip to content

Instantly share code, notes, and snippets.

@bavington
Created January 5, 2014 11:13
Show Gist options
  • Save bavington/8266985 to your computer and use it in GitHub Desktop.
Save bavington/8266985 to your computer and use it in GitHub Desktop.
Fixes the problems (post WordPress 3.1) with Custom Post Type Menu classes not being added correctly.
<?php function remove_parent_classes($class)
{
// check for current page classes, return false if they exist.
return ($class == 'current_page_item' || $class == 'current_page_parent' || $class == 'current_page_ancestor' || $class == 'current-menu-item') ? FALSE : TRUE;
}
function add_class_to_wp_nav_menu($classes)
{
switch (get_post_type())
{
case 'projects':
// we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items.
$classes = array_filter($classes, "remove_parent_classes");
// add the current page class to a specific menu item (replace ###).
if (in_array('menu-item-35', $classes))
{
$classes[] = 'current_page_parent';
}
break;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment