Home › Forums › Add-ons › Repeater Field › Display a checkbox value within a repeatable field › Reply To: Display a checkbox value within a repeatable field
Jason, when you are retrieving a value from a checkbox, it’s returned an array of value, since the check box can have multiple choices.
Following your code above, since checkbox can have multiples values, sometimes when the user select 2 checkboxes your code will not work.
I recommend you to use the option Radio Button, then the user can select only one option only. If you still want to continue with the checkbox, you can do it by this way:
<?php if( have_rows('checkbox_repeater') ): ?> <?php while ( have_rows('checkbox_repeater') ) : the_row(); ?> <?php $color = get_sub_field('checkbox'); $background = get_sub_field('background'); $content = get_sub_field('content'); echo "color = "; var_dump($color); // just for debugging ?> <br /> <br /> <section style="background-color:<? echo $background; ?>; color: <?php echo $color; ?>; height:40px; width:100%;"> <?php echo $content; ?> </section><br /><br /><br /> <?php endwhile; ?> <?php endif; ?>
The result of this code is on the file annexed.
If you want to know more about the checkbox field, you can check the docs.
Here is the link:
To display the radio button, you don’t need to specify the array number, because for radio buttons it’s returned a normal string.
$radio_field = get_sub_field('radio'); echo $radio_field; // returned: #CCCCCC var_dump($radio_field); // debug, returned: string(4) "#ccc"
Have a nice day! =D
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
2022 was a big year for Advanced Custom Fields, with more capabilities, a new generation of ACF Blocks, a refreshed UI, and a new home. Our year in review post looks at advancements we’ve made and offers a glimpse of the future.— Advanced Custom Fields (@wp_acf) January 6, 2023
© 2023 Advanced Custom Fields.