Get the label from ACF checkbox [closed]

I feel like this should be easy but for some reason I can’t get this to work?

I have several similar but distinct colors in list of products. I need to be able to grap the label to show to the end user (so they see orange instead of m_orange) but I also need the value to be distinct so I can apply the appropriate class to the element.

orange : orange
m_orange : orange
purple : purple
m_purple : purple
w_purple : purple

My code:

<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$color = get_field('color');
?>

<?php foreach($color as $color_list){echo "<li >$color_list <span class=\"color_text_slash\"> / </span> </li>";}?>  

 <?php foreach($color as $color_class){echo "<li class=\"$color_class\"></li>" ;}?>

I tried to follow instructions at http://www.advancedcustomfields.com/resources/field-types/checkbox/ but it hasn’t worked and I can’t seem to figure this out?

2 Answers
2

You can try this

$field = get_field_object('color'); 
$colors = get_field('color'); // array of selected color values 
foreach($colors as $color){
    echo "selected color: ". $color. " with label: " . $field['choices'][ $color ];
}

where the labels are fetched by get_field_object according to the link you provided.

You could also use print_r() or var_dump() to check what is stored in the variables $field and $colors.

Leave a Comment