1

Question edited

How can I add a class to the body when the menu at the menu setting > theme locations is selected.

Like in this screenshot:

theme locations

And if I have two menus, can it add two different classes to the body tag?

I would like to use that class to control the nav toggle icon, etc.

2
  • Please edit to explain what exactly do you mean in "primary-menu is enabled".. Do you mean added, show, hovered on or something totaly different Nov 22 '15 at 7:36
  • I guess you used jQuery function to enable or disable the primary-menu. So just use the same function add a class to the body.
    – Kvvaradha
    Nov 22 '15 at 7:46
2

Use the has_nav_menu to detect if the menu is configure in a body_class filter.

Something like

add_filter( 'body_class', 'wpse209469_body_class' );
function wpse209469_body_class( $classes ) {

    if (has_nav_menu('primary-menu'))
      $classes[] = 'primary-menu';

    return $classes;
}

If you have two menus you can change the condition to match both, or just do two filters. There is probably a way to enumerate all the menus and add classes for them but it is probably an overkill.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.