Kaur16
Kaur16

Reputation: 11

ACF returning checkbox values multiple times

I'm building a website in wordpress with the help of ACF but i've ran into a problem i can't seem to wrap my head around.

I have a repeater field called "portfolio" with a checkbox field with 3 choices. I use them so i can add classes to div's for filtering purposes and it works fine but when i wanted to display choice values to my webpage as list then it gives me the same values multiple times.

For example i have 5 div's added and 3 of them have checked value of "digi" so when i call the checkbox values then i get the list element "digi" 3 times. I really only want to display 3 different choices to my website.

My code:

<?php

if( have_rows('portfolio') ):
 while ( have_rows('portfolio') ) : the_row();
   $class = get_sub_field('portfolio_img_class');



 foreach( $class as $value ): ?>
    <li><?php echo $value; ?></li>
 <?php endforeach;               


 endwhile;
 else :
   echo "FFS";
 endif;
?>

The other code i use for giving classes to the divs:

<?php if( have_rows('portfolio') ): ?>

                <section class="work">

                <?php while( have_rows('portfolio') ): the_row(); 

                    $image = get_sub_field('portfolio_img');
                    $text = get_sub_field('portfolio_img_text');
                    $link = get_sub_field('portfolio_img_link');
                    $class = get_sub_field('portfolio_img_class');

                    ?>

                    <div class="item-work item-work-portfolio 

                                <?php 
                                foreach ( $class as $value ) {  echo $value . " ";  } ?> 

                                "> 

                        <a href="<?php echo $link; ?>">
                            <img src="<?php echo $image; ?>"/>
                            <div class="work-overlay">
                                <h1 class="work-overlay-title"><?php echo $text; ?></h1>
                            </div>
                        </a>
                    </div> 

                <?php endwhile; ?>

                </section>

            <?php endif; ?>

I have tried so many things but nothing seems to work. I'm this close to just hard-code the damn things in the theme.

Edit: The output of the first code:

<ul id="nupud">
 <li><a class="btn" onclick="filterSelection('all')">Kõik</a></li>                                                                        
 <li>digi</li>                                                                               
 <li>disain</li>                                                                               
 <li>digi</li>                                                                                    
 <li>disain</li>                                                                                           
 <li>digi</li>                                                                                           
 <li>disain</li>                                                                                           
 <li>digi</li>                                                                                           
 <li>disain</li>                                                                             
 <li>digi</li>                                                                                          
 <li>disain</li>                                                                                         
 <li>sotsiaalmeedia</li>
</ul>

Upvotes: 0

Views: 654

Answers (1)

user1247034
user1247034

Reputation:

So you can store all the values in a single array, then use another loop on this single array, the array_unique() function to remove duplicate values and the array_values() function to reindex the array. Then output the unique values:

<?
$allValues = array();
if (have_rows('portfolio')):
    while (have_rows('portfolio')) : the_row();
        $class = get_sub_field('portfolio_img_class');
        foreach ($class as $value):
            $allValues[] = $value;
        endforeach;
    endwhile;
else:
    // do nothing
endif;
$uniqueValues = array_values(array_unique($allValues));
foreach ($uniqueValues as $value):
    echo $value . ' ';
endforeach;

Upvotes: 1

Related Questions