dongyu3712 2017-12-23 00:32
浏览 28
已采纳

WordPress ACF复选框循环

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; ?>
  • 写回答

1条回答 默认 最新

  • dpx86402 2017-12-23 00:43
    关注

    Use an array.

    <?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; ?>
    

    or like

    <?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; ?>
    

    or get rid of the if statement and use 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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联