I'm trying to add a class to the body tag based on whether current menu items either have children, or are children. The custom menu was added via wp_nav_menu
.
I have hooked into wp_nav_menu_objects
via a filter, and am successfully detecting whether the current active menu item is a ancestor or child:
function has_submenu( $menu_items ) {
$current_id = 0;
foreach( $menu_items as $menu_item ) {
// Get the id of the current menu item
if( $menu_item->current ) {
$current_id = $menu_item->ID;
}
// if the current item has a child
if( $menu_item->menu_item_parent != 0 && $menu_item->menu_item_parent == $current_id ) {
$body_class = 'has-submenu';
break;
}
// if the current item has an ancestor
if( $menu_item->current_item_ancestor ) {
$body_class = 'is-submenu';
break;
}
}
return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'has_submenu', 10, 2 );
Now the issue i'm having is how to I then filter body_class
to add the class?? I created the variable called $body_class, however i'm not sure hot to pass it to the body_class
hook function. Make it a global variable? Create a class for all this?
Thanks in advance!
'is-submenu'
or'has-submenu'
to the body class, we can help you find a different solution.