WordPress ACF复选框循环

问题描述 投票:0回答:1

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

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

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

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

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

任何建议表示赞赏,谢谢。

<?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; ?>
php arrays wordpress checkbox advanced-custom-fields
1个回答
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; ?>
© www.soinside.com 2019 - 2024. All rights reserved.