Body Classes From ACF True/False Fields
Adds a body class such as "acf-hide-title" when a true/false field is switched on for the current page, so you can change the layout with CSS alone.
Opens WPIDE on
Code
// True/false field names that become body classes.
$wpide_acf_class_fields = array( 'hide_title', 'full_width', 'transparent_header' );
add_filter( 'body_class', function ( $classes ) use ( $wpide_acf_class_fields ) {
if ( ! is_singular() || ! function_exists( 'get_field' ) ) {
return $classes;
}
foreach ( $wpide_acf_class_fields as $name ) {
if ( get_field( $name, get_queried_object_id() ) ) {
$classes[] = 'acf-' . sanitize_html_class( str_replace( '_', '-', $name ) );
}
}
return $classes;
} );
Notes
List your true/false field names. The class is "acf-" plus the field name with dashes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.