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’?
The only way i could find is to use
wp_get_nav_menu_items($menuID)
i tested it below with my menu
id
of7
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 aforeach
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 likeEdited for Current Classes only
input to the functions.php page
the documentation is here
if you want to add a custom class to the body tag simply use the body class,
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
you could also change the class based on category name.
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