Skip to content

Instantly share code, notes, and snippets.

@mrkkr
Created April 5, 2019 10:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mrkkr/46edfe1e6839646d57007cf5deebf0d6 to your computer and use it in GitHub Desktop.
Add top level menu active menu class to child pages without adding submenu level in admin panel
<?php
function additional_active_item_classes( $classes = array(), $menu_item = false ) {
// custom taxonomy
if ( $menu_item->title == 'Custom Tax Name Page' && is_tax('custom_tax') ) {
$classes[] = 'current-menu-item';
}
// custom post type single
if ( $menu_item->title == 'Custom Post Type Page' && is_singular('products') ) {
$classes[] = 'current-menu-item';
}
// blog post single
if ( $menu_item->title == 'Blog Page' && is_singular('post') ) {
$classes[] = 'current-menu-item';
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'additional_active_item_classes', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment