WordPress高级自定义字段-按子字段值的随机查询中继器 [英] WordPress Advanced Custom Fields - Random Query Repeater by SubField value

查看:118
本文介绍了WordPress高级自定义字段-按子字段值的随机查询中继器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取一个 随机行推荐,但是该行必须包含 sitewide_display 子字段的 true值。

I'm trying to fetch ONE random row of testimonials, however that row must contain a "true" value for sitewide_display subfield.

我不能一辈子都可以使用它,这给了我刷新后的感觉。

I can't for the life of me get this to work, this just gives me whatever result it feels like upon refresh.

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

Is there some sort of conflict with using the conditional for the sub-field value (sitewide_display) within the while loop like this?

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


推荐答案

我用以下方法解决了这个问题

I have solved the issue with the following, if there is a more efficient way go ahead and comment!

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


    }
}


?>

这篇关于WordPress高级自定义字段-按子字段值的随机查询中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆