WordPress ACF复选框循环

时间:2017-12-23 00:32:41

标签: php arrays wordpress checkbox advanced-custom-fields

我目前有'餐厅'的CPT。 每个餐厅都可以有多个“功能”,由用户通过ACF复选框字段在后端选择。

我希望能够遍历此字段并为每个选中的复选框应用类名。

在下面的例子中,餐厅有4个可能的字段中的3个。 但是,只有“停车”图标显示(4次) - 可能是由于我的if语句。

理想情况下,我希望循环遍历每个'if',抓住连接到$ feature_icon的字符串,然后将其吐出到跨度下方的回声中。

我已经尝试将if语句的部分移到'foreach'循环中但无济于事。

感谢任何建议,谢谢。

Promise {$$state: {…}}

1 个答案:

答案 0 :(得分:1)

使用数组。

<?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'] = "Alcohol";
    endif;

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

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

    if(in_array('parking', $restaurant_features)):
        $feature_icon['parking'] = "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[$restaurant_feature]; ?>"></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; ?>

或喜欢

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

<div class="card__inputs"> <!-- Featured card specific icons start -->
    <?php
    $feature_icon = [
        'alcohol' => "Alcohol",
        'family_friendly' => "Family-Friendly",
        'open_late' => "Open-Late",
        'parking' => "Parking",
    ];
    ?>
    <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[$restaurant_feature]; ?>"></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; ?>

或删除if语句并使用str_replace()

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

<div class="card__inputs"> <!-- Featured card specific icons start -->
    <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): ?>
        <?php $icon = str_replace(' ', '-', ucwords(str_replace('_', ' ', $restaurant_feature))); ?>
            <span class="icon icon--medium icon--<?php echo $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; ?>