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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

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

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

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

 &lt;?php if($ featured_restaurants):?&gt; 
&lt;?php foreach($ featured_restaurants as $ featured_restaurant):?&gt; 
 
&lt; div class =“card__inputs”&gt;  &lt;! - 特色卡片特定图标开始 - &gt; 
 
&lt;?php 
 if(in_array('alcohol',$ restaurant_features)):
 $ feature_icon =“Alcohol”; 
 endif; \  n 
 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; 
?&gt; 
  
&lt; div class =“details u-float-left”&gt;  &lt;! - 特色卡片特定图标容器开始 - &gt; 
&lt; span class =“icon icon  -  medium icon  -  Italian”&gt;&lt; / span&gt; 
&lt;?php if($ restaurant_features  ):?&gt; 
&lt;?php foreach($ restaurant_features as $ restaurant_feature):?&gt; 
&lt; span class =“icon icon  -  medium icon  - &lt;?php echo $ feature_icon;?&gt;  “&gt;&lt; / span&gt; 
&lt;?php endforeach;  ?&gt; 
&lt;?php endif;  ?&gt; 
 
&lt; / div&gt;  &lt;! - 特色卡片特定图标容器端 - &gt; 
 
&lt; / div&gt;  &lt;! - 特色卡片特定图标结束 - &gt; 
 
&lt;?php endforeach;  ?&gt; 
&lt;?php wp_reset_postdata();  ?&gt; 
&lt;?php endif;  ?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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; ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题