如果管理面板中没有嵌套菜单结构,则此代码将类“当前菜单祖先”添加到子CPT的父项菜单或自定义分类法或默认单篇文章中-仅当您有“0级”菜单时。例如,如果您有显示产品网格的页面Product,并且您转到单个产品,则WP将看不到父菜单项。下面的代码改进了这一点:
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-ancestor\';
}
// custom post type single
if ( $menu_item->title == \'Custom Post Type Page\' && is_singular(\'products\') ) {
$classes[] = \'current-menu-ancestor\';
}
// blog post single
if ( $menu_item->title == \'Blog Page\' && is_singular(\'post\') ) {
$classes[] = \'current-menu-ancestor\';
}
return $classes;
}
add_filter( \'nav_menu_css_class\', \'additional_active_item_classes\', 10, 2 );