Skip to content

Instantly share code, notes, and snippets.

@tammyhart
Created April 9, 2014 08:31
Show Gist options
  • Save tammyhart/10241693 to your computer and use it in GitHub Desktop.
Save tammyhart/10241693 to your computer and use it in GitHub Desktop.
Highlight correct menu item for custom post types
add_filter( 'nav_menu_css_class', 'thd_menu_classes', 10, 2 );
function thd_menu_classes( $classes , $item ){
if ( get_post_type() == 'event' || is_archive( 'event' ) ) {
// remove that unwanted classes if it's found
$classes = str_replace( 'current_page_parent', '', $classes );
// find the url you want and add the class you want
if ( $item->url == '/events' ) {
$classes = str_replace( 'menu-item', 'menu-item current_page_item', $classes );
remove_filter( 'nav_menu_css_class', 'thd_menu_classes', 10, 2 );
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment