How to add the optional menu css class to body_class?

I’m trying to figure out how to add the menu class (optional field in admin) to the body_class, as I want to use the menu item’s color on other elements of the page:

/** Add nav menu css class to body class */
function add_nav_menu_css( $classes ) {
    $classes[] = ‘optional-menu-class’;
    return $classes;
}
add_filter( ‘body_class’, ‘add_nav_menu_css’ );

What should replace ‘optional-menu-class’?

Related posts

Leave a Reply

2 comments

  1. The only way i could find is to use wp_get_nav_menu_items($menuID)

    i tested it below with my menu id of 7

    $items = wp_get_nav_menu_items( 7 );
    foreach ($items as $i) {
        echo $i->classes[0];
    }
    

    outputs sample-page-class which I inputed on the appearance menu page.

    All of the menu classes are stored in the function wp_get_nav_menu_items as classes so then you can just loop them out with a foreach like i did above.

    and then transfer that data to the body_class.

    Further Explained:

    So your body_class filter in the functions.php should like

    add_filter('body_class','add_nav_menu_css');
    function add_nav_menu_css($classes) {
    
            $items = wp_get_nav_menu_items( 7 ); // change to id of your menu
    
            foreach ($items as $i) {
                    $menuClasses .= $i->classes[0].' ';
            }
    
            $classes[] = $menuClasses;
    
    
            return $classes;
    }
    

    Edited for Current Classes only

    input to the functions.php page

    add_filter('body_class','my_class_names');
    function my_class_names($classes) 
    {
            $items = wp_get_nav_menu_items( 7 ); //change to suit your menu id
              foreach ($items as $item):
                    $menuClasses = $item->classes;
                    $objectId = $item->object_id.' ';
    
                    if ( is_page($item->object_id) ):
                        $current[] = $menuClasses;
                    endif;
    
    
    
              endforeach;
    
            $classes[] =  $current[0][0];
    
    
            return $classes;
    }
    

    the documentation is here

  2. if you want to add a custom class to the body tag simply use the body class,

    <body <?php body_class('class-name'); ?>>
    

    where ‘class-name’ is your custom class,
    if you want this to change based on or page/post, simply add this to your header.php

    <?php
    if(is_page('your-page-name')){// or post
      $body_class = 'your-custom-page-class';
    }
    ?>
    <body <?php body_class($class_name); ?>>
    

    you could also change the class based on category name.

    <?php
    if( !is_front_page() && is_category() || is_single() ){
       // category single name
       $cat_name = single_cat_title('',false);
       // category id from category name
       $cat_id = get_category_id($cat_name);
       $body_class = $cat_name;
    }
    ?>
    <body <?php body_class($body_class); ?>>
    

    hopefully that works out for u.

    M

    ps: just seen your update
    have a look here,
    https://wordpress.stackexchange.com/questions/20110/add-custom-class-to-wp-nav-menu-using-filter-hook-nav-menu-css-class