Home › Forums › Front-end Issues › Mutliple choice checkbox, add a class
Hi there. I created the checkbox with three choices:
Option1
Option2
Option3
I’m trying to check this values in array to define the string variable $addclassexamgrid, that is sent to a class (through echo).
Here is the code:
<?php
// check if the repeater field has rows of data
if( have_rows('add_info_exam_grid') ): ?>
<?php $checkboxarray = get_field('add_age_exam_grid'); ?>
<?php if (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option2", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>
<?php $addclassexamgrid = 'class1 class2 class3'; ?>
<?php elseif (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option2", $checkboxarray))) : ?>
<?php $addclassexamgrid = 'class1 class2'; ?>
<?php elseif (($checkboxarray && in_array("Option1", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>
<?php $addclassexamgrid = 'class1 class3'; ?>
<?php elseif (($checkboxarray && in_array("Option2", $checkboxarray)) and ($checkboxarray && in_array("Option3", $checkboxarray))) : ?>
<?php $addclassexamgrid = 'class2 class3'; ?>
<?php elseif ($checkboxarray && in_array("Option1", $checkboxarray)) : ?>
<?php $addclassexamgrid = 'class1'; ?>
<?php elseif ($checkboxarray && in_array("Option2", $checkboxarray)) : ?>
<?php $addclassexamgrid = 'class2'; ?>
<?php elseif ($checkboxarray && in_array("Option3", $checkboxarray)) : ?>
<?php $addclassexamgrid = 'class3'; ?>
<?php endif; ?>
<?php while ( have_rows('add_info_exam_grid') ) : the_row(); ?>
<div class="grid-item <?php echo $addclassexamgrid; ?> col-md-4 col-sm-6 col-xs-12">
//Some code here
</div>
<?php endwhile;
else :
// no rows found
endif;
?>
The problem is that the value of the variable $addclassexamgrid is not send to div class. Could anybody help?
What is the return value of the field set to? Your code assumes values, but you might want to check it. It could be somewhere in you if/elseif statements.
I would simplify it like this
<?php
if (have_rows('add_info_exam_grid')) {
$addclassexamgrid = array();
$checkboxarray = get_field('add_age_exam_grid');
foreach ($checkboxarray as $value) {
switch ($value) {
case 'Option1':
$addclassexamgrid[] = 'class1';
break;
case 'Option2':
$addclassexamgrid[] = 'class2';
break;
case 'Option3':
$addclassexamgrid[] = 'class3';
break;
} // end switch
while (have_rows('add_info_exam_grid')) {
the_row();
?>
<div class="grid-item <?php echo implode(' ', $addclassexamgrid); ?> col-md-4 col-sm-6 col-xs-12">
//Some code here
</div>
<?php
} // end while have_rows
} // end foreach
} // end if have_rows
?>
You must be logged in to reply to this topic.
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!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.