Adding a body class with ACF

Hook into the body_class filter and add your field there. It might be better to get the ID from get_queried_object_id() instead of get_the_ID().

add_filter( 'body_class', 'wpse_20160118__body_class' );

function wpse_20160118__body_class( $classes ) {

    if ( $package_colour = get_field( 'package_colour', get_queried_object_id() ) ) {

        $package_colour  = esc_attr( trim( $package_colour ) );

        $classes[]       = $package_colour;
    }

    return $classes;
}

Leave a Comment