0

我正在尝试获取一个随机的推荐行,但是该行必须包含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; ?>
4

1 回答 1

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


    }
}


?>
于 2014-02-25T19:08:13.280 回答