This is probably what you are looking for ..
add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' );
function add_menu_parent_class( $items ) {
$parents = array();
foreach ( $items as $item ) {
if ( in_array('current-post-ancestor', $item->classes) ) {
$parents[] = $item->menu_item_parent;
}
}
foreach ( $items as $item ) {
if ( in_array( $item->ID, $parents ) ) {
$item->classes[] = 'current-menu-ancestor';
}
}
return $items;
}
It will add a class to current post ancestor menu item parent. I have tried this in my theme and it is working perfectly.
Related Posts:
- Display a portion/ branch of the menu tree using wp_nav_menu()
- how to create a menu with all sub categories?
- Display only page specific sub menu items using Custom Walker
- Add a .last class to the last in each ul.sub-menu
- Multi Level Bootstrap Navigation Menu in WordPress
- Add class to top level menu item if it has multiple child levels
- Show only 2nd level of navigation depending on active navigation
- Simple Navigation Walker – Wrapper-class around first sub-menu
- Exclude one item from wp_list_pages( $args );
- wp_nav_menu() loses ‘current-menu-*’ classes on single product page within category
- How to drag multiple menu items at once in WordPress?
- Customizing the a tag with Semantic UI
- How to make pages metabox display all hierarchy in menu admin?
- Add items to a menu dynamically
- How can I add a sub menu to an existing navigation menu in WordPress?
- Custom Nav Walker sub-menu HTML construct
- Add div to specific sub-menu
- Automatically Add New Tags to Sub-menu
- Adding different classes to anchor in navigation menu
- how to only add a top-level admin menu without it creating a sub-level-menu
- how to automatically generate hierarchical menus from hierarchy of pages?
- Custom Walker for Walker_Nav_Menu
- Arrow down in menu not displaying
- Handling complex multi-level architecture / menu for large site
- Update Nav Menu Items Programmatically
- How to display dynamic content in start_lvl function
- Menus like a CMS
- next_post_link() on custom menu structure
- How to add a submenu toggle button inside all “li” elements that have a submenu?
- Check if menu id = $specific_id – then insert specific
- Show menu based on parent & ancestor
- How to include a third level with wp_get_nav_menu_items function
- wp_nav_menu() with multiple Class
- Schedule Page to Menu [duplicate]
- How do you manage large menu hierarchies in WP?
- Update body class based on menu
- Trying to Create Menu in WordPress
- Hierarchical menu nesting problem with jQuery targetting
- Automatically list top level menu item child pages
- Check if menu_item has children or has parent
- Add code to WordPress menu items by class
- Creating a menu containing categories and authors
- Change submenu markup
- How to customize submenus isn wordpress theme [closed]
- Switch from hover to click on Twenty Twelve Menu
- a better menu, menu options assistance for WP
- Can I have a different menu for each parent page?
- remove class “sub-menu” from wordpress drop down menu
- Add a submenu to wp_nav_menu
- Wrap list parent in div with wp_nav_menu menu
- Insert a div class inside wordpress menu
- Automatically Display Sub Menu
- sub menu link is not working
- Get parents child pages
- How to get submenu of admin menu?
- How to Add support for sub-menu item description in wordpress?
- Add menu headings to WordPress sub menus
- Removing Submenu from Menu
- An alternative to WordPress’s built-in menu functionality
- Dropdown primary navigation menu not working
- Is there any way to change the CSS class applied to 2nd level sub menu ul’s?
- Link after WordPress
- cannot get global $menu value in form request
- Show Sub Menu on click and not hover
- Add Different Class on Different Ul in twenty-twentyone theme
- insert an HTML element if there is a submenu only
- Change hover to onclick on menu
- wp_nav_menu container style args
- Bootstrap Theme Migrated to WordPress
- having a page in multi sub-menu and match current-menu-item
- How can I click the parent menu then redirect to a special URL?
- Why my dropdown menu is effecting main menu?
- Creating navigation out of specific IDs and their children?
- Where to place a svg chevron down in my custom walker?
- How to access into the description of a sub menu
- Generate Submenu based on Parent Page using If Statement
- WordPress menu issue after update
- Product categories are mixed when creating menu
- Adding span to a particular text in an menu item
- Displaying a part of a menu containing the page the user is currently viewing and only it’s immediate children
- how to create pages and link them to a menu
- Conditional Nav Menu Inside Nav Menu Across Multisite Sites
- How to separate sub-menu output and keep associated with parent
- Has there been any improvements to wp’s menu handling?
- Show Child of Parent Page including all other parents
- How to display list of child pages with parent in wordpress?
- nav_menu: how to force a subpage switching on the parent menu
- How to create a menu super voice in WP?
- Display all submenus
- Change sub-menu css class
- Adding Sub Menu In a theme which Do not have Menu Options
- ID of parent Menu
- How can I alter the visible menu according to which level I am on [closed]
- How was this menu system created?
- How to display a submenu as a popup when clicking a button?
- how to mage the submenu open on wordpress active page
- Display sub menus as separate lists
- WordPress menu is getting automatically nested
- Add acf custom fields so sub item in menus
- Give custom class names to WP nav sub-menu