1
votes

I want to change the class when choosing a checkbox inside the repeater. I do this with a switch case, but I cannot get the checkboks value outside while. How do I get the checkbox array out of while correctly?                                       

ACF

$checkboxarray = get_field('sy_color');

Code:

<div class="loop solution-include-pages d-flex">
    <?php
    if (have_rows('support_you')) {
        $checkboxarray = get_field('sy_color');
        foreach ($checkboxarray as $value) {
            switch ($value) {
                case 'red':
                    $class = 'rose-c';
                    break;
                case 'green':
                    $class = 'green-c';
                    break;
                case 'blueberry':
                    $class = 'blueberry-c';
                    break;
            }
            while (have_rows('support_you')) {
                the_row();
                $sy_img = get_sub_field('sy_img');
                $sol_img = wp_get_attachment_image_src( $sy_img, 'full', true );
                $sy_header = get_sub_field('sy_header');
                $sy_content = get_sub_field('sy_content');
                $sy_link = get_sub_field('sy_link');
                $sy_color = get_sub_field('sy_color');
    ?>
    <div class="sol-item d-flex flex-direction">
        <div class="sol-item-img">
            <img src="<?php echo $sol_img[0];?>" class="img-responsive" alt="">
        </div>
        <div class="sol-content">
            <div class="sol-content-header <?php echo $class; ?>">
                <?php echo $sy_header; ?>
            </div>
            <div class="sol-content-text">
                <?php echo $sy_content; ?>
            </div>
            <a href="<?php echo $sy_link;?>" class="bluberry-btn item-center">Learn more</a>
        </div>
    </div>
    <?php
    }
    }
    }
    ?>
</div>

Acf checkboks option:

Acf checkboks option

1
If the checkbox is inside the repeater, you should use get_sub_field() instead of get_field()denisey

1 Answers

1
votes

If you are not within the_row(); you cannot get a sub field. If you assign the field "support_you" to a variable using

$support_you = get_field('support_you');

It will return an array of arrays, all the sub fields will be returned in that array you can access it that way. A good way to see the structure of what is returned is by echoing the array to the screen like this

echo '<pre>', print_r( $support_you, 1 ), '</pre>';