I need the WordPress menu to include the ‘current-menu-ancestor’ class to reflect that site is currently in the recipe section. Supposing I have a recipe custom post type. I have the following code in my functions.php but it’s not working:
function add_active_item_classes($classes = array(), $menu_item = false){
if ( get_post_type() == 'recipe' && $menu_item->title == 'Recipes') {
$classes[] = 'current-menu-ancestor';
return $menuclasses;
}
}
Also I don’t know what filter hooks I will use to have this effect? Thanks for your suggestion and assistance.
This is the final working code:
This code add class ‘current-menu-ancestor’ to parent item menu of your child CPT or custom taxonomy or default single post, in case you do not have a nested menu structure in the admin panel – only if you have ‘level 0’ menu. For example – if you have Page Product, which display products grid and you go to the single product – WP will not see the parent menu item. The code below improve this: