Home › Forums › General Issues › Adding custom CSS class in body_class()
Hi!
I would like to add a custom CSS class in the body_class()
My code in the function.php looks like:
function add_acf_body_class($class) {
$field = get_field_object('page_layout', get_queried_object_id());
$value = get_field('page_layout');
$label = $field['choices'][$value];
$class[] = $label;
return $class;
}
add_filter('body_class', 'add_acf_body_class');
I have created a custom field of Select type with several choices: left, right
Can someone help me?
Thanks in advance!
Martin
If your select field is returning the value, is there a reason you want to set the class to the label?
I don’t see anything in your code that would make it not work. Is it adding anything to the body class?
What type of page are you looking at? Is is a single page/post or something else?
Thank you for your response and sorry for my late 😉
So you mean, I can solve this like that:
function add_acf_body_class($class) {
$field = get_field_object('page_layout', get_queried_object_id());
$value = get_field('page_layout');
$label = $field['choices'][$value];
return $label;
}
add_filter('body_class', 'add_acf_body_class');
It’s a post. And nothing is added to the body class.
This should work to add the value
function add_acf_body_class($class) {
$value = get_field('page_layout');
$class[] = $value;
return $class;
}
add_filter('body_class', 'add_acf_body_class');
I don’t see any reason why it wouldn’t. What kind of field is "page_layout"
is it just a select field or is it a sub field of of something else?
Field Group: layout
Field Label: Page Layout
Field Name: page_layout
Field Type: Select
Choices: left, right
I have tested your code, it doesn’t work for me.
I don’t understand what you are telling me.
Is the select field part of a “Group” field?
You have to get the “Group” field and then get the value of your select field from the array that is returned. See the code examples for the Group field https://www.advancedcustomfields.com/resources/group/
Well, thanks again.
I think I won’t find a solution.
I have misunderstood you. It isn’t a group filed, it’s a select field type.
It could be that ACF does not know where to get the field from. the body tag hook happens outside of the loop.
function add_acf_body_class($class) {
$queried_object_id = get_queried_object_id();
$value = get_field('page_layout', $queried_object_id);
$class[] = $value;
return $class;
}
add_filter('body_class', 'add_acf_body_class');
Could you just add it to template file?
<?php $value = get_field(‘page_layout’); ?>
<body class=”<?php echo $value; ?>”>
The topic ‘Adding custom CSS class in body_class()’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.