Roots Discourse

Trouble on filter body_class with acf field

HI guys, i’m trying to filter body_class to display a class if an acf field, in a wp block, exists.

I have a fieldset with a gallery field called ‘gallery_block’ inside.
I created the wpBlock through sage-acf-gutenberg-blocks v0.5.
Everything works but i need to add a class to body:
so on filters.php

add_filter(‘body_class’, function (array $classes) {

$gallery = get_field(‘gallery_block’, get_queried_object_id());

if (!empty($gallery) {
$classes[] = ‘enable-slider’;
}


return array_filter($classes);

and now i’m stuck here because the class doesn’t show up.
Thanks for any suggestion.

I found the solution … and it was also easy … damn!

just checking if block exists and not the field.

if (has_block('acf/gallery')) {
    $classes[] = 'enable-gallery';
}

This topic was automatically closed after 42 days. New replies are no longer allowed.