Use ACF select field to add a class to div inside a flexible content layout [closed]

You should show your whole code. What you’ve posted above won’t output at all. Additionally there is no need to assign a variable as you have a value already…

your field values are set in ACF this way for a radio button:

white : White
light-grey : Light Grey
dark-grey : Dark Grey

(notice the space around the colon)

Now for your code:

$bg = get_field('background', $post->ID);
if( $bg ) {
    $background = $bg['value'];
} else {
    $background ='';
}

echo '<div class="mybackground '.$background.'">hi there</div>';

then your css:

.white {
 background-color:white;
}
.dark-grey {
 background-color:grey;
}
.light-grey {
 background-color:#f0f0f0;
}

You will need to customize a bit for what you’re actually doing but this should work.

If you have format value set to just return value then change this line:

$background = $bg['value'];

to this:

$background = $bg;