I have created a radio button field with ACF so that admin can specify a colour. E.g:

  • blue : Blue
  • pink : Pink

I’d like this to be output as a class via <body <?php body_class(); ?> in header.php. Is this possible?

I’m currently outputting the class in header.php like so:

<div id="page" class="site <?php the_field('package_colour',$post->ID); ?>">

1 Answer
1

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 Reply

Your email address will not be published. Required fields are marked *