【问题标题】:WordPress Advanced Custom Fields - Random Query Repeater by SubField valueWordPress 高级自定义字段 - 按子字段值的随机查询重复器
【发布时间】:2014-03-27 12:03:59
【问题描述】:

我正在尝试获取 ONE 随机 行推荐,但是该行必须包含 sitewide_display 子字段的“真实”值。

我这辈子都无法让它工作,这只会给我刷新后的任何感觉。

像这样在 while 循环中使用子字段值 (sitewide_display) 的条件是否存在某种冲突?

<?php $rows = get_field('testimonials' ); // get all the rows ?>
    <?php if( $rows ) : // if there are rows, continue ?>  
         <?php while( has_sub_field('testimonials') ) : ?>   
            <?php if( get_sub_field('sitewide_display')): ?> 
                <?php $rand_row = $rows[ array_rand( $rows ) ]; // get the first row ?>
                <?php  $rand_row_testimonial_name = $rand_row['testimonial_name' ]; // get the sub field value  ?>
                <?php echo $rand_row_testimonial_name; ?>            
            <?php endif; ?> 
    <?php endwhile; ?>
<?php endif; ?>

【问题讨论】:

    标签: php loops wordpress advanced-custom-fields


    【解决方案1】:

    我已经解决了以下问题,如果有更有效的方法请继续评论!

    <?php
    
    $lists = get_field( 'testimonials' );
    shuffle($lists);
    if( $lists ){
    
        $i=0;
        foreach( $lists as $list ){
            if( $list['site-wide_display'] &&  $i < 1){
                echo $list['testimonial_name'];
                $i +=1;
            }
    
    
        }
    }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-26
      • 2018-02-04
      • 2020-04-26
      • 2018-08-15
      • 2015-10-24
      • 2021-04-30
      • 2016-06-25
      • 2012-08-10
      相关资源
      最近更新 更多