vendredi 22 décembre 2017

WordPress ACF Checkbox loop

I currently have a CPT of 'restaurant'. Each restaurant can have multiple 'features', chosen by the user in the back end via a ACF checkbox field.

I'd like to be able to loop through this field and apply a class name for each checkbox that is checked.

In the below example, the restaurant has 3 of the 4 possible fields ticked. However, only the 'Parking' icon is showing (4 times) - likely due to my if statements.

Ideally, i'd like it to loop through each 'if', grab the string attached to $feature_icon and then spit it out in the echo further below in the span.

I've tried moving the section of if statements to be within the 'foreach' loop to no avail.

Any advice appreciated, thanks.

<?php if( $featured_restaurants ): ?>
<?php foreach ( $featured_restaurants as $featured_restaurant ): ?>

<div class="card__inputs"> <!-- Featured card specific icons start -->

<?php
    if(in_array('alcohol', $restaurant_features)):
        $feature_icon = "Alcohol";
    endif;

    if(in_array('family_friendly', $restaurant_features)):
        $feature_icon = "Family-Friendly";
    endif;

    if(in_array('open_late', $restaurant_features)):
        $feature_icon = "Open-Late";
    endif;

    if(in_array('parking', $restaurant_features)):
        $feature_icon = "Parking";
    endif;
?>

    <div class="details u-float-left"> <!-- Featured card specific icons container start -->
    <span class="icon icon--medium icon--Italian"></span>
    <?php if($restaurant_features): ?>
        <?php foreach($restaurant_features as $restaurant_feature): ?>
            <span class="icon icon--medium icon--<?php echo $feature_icon; ?>"></span>
        <?php endforeach; ?>
    <?php endif; ?>

    </div> <!-- Featured card specific icons container end -->

    </div> <!-- Featured card specific icons end -->

<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>




Aucun commentaire:

Enregistrer un commentaire