get wordpress menu custom css classes and add it to body classes

 

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

$menu_name = 'primary'; // menu theme location
global $wp_query;
$queried_object = $wp_query->get_queried_object();
$current_id = $queried_object->ID; 
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
        $items = wp_get_nav_menu_items($menu->term_id);

        foreach ($items as $i) {
                if($current_id == $i->object_id){
					$menuClasses .= $i->classes[0].' ';
				}
				
        }

        $classes[] = $menuClasses;

}
        return $classes;

}

Leave a Reply

Your email address will not be published. Required fields are marked *