Repeater高级自定义字段中的随机中继器

时间:2015-09-30 15:40:58

标签: wordpress advanced-custom-fields

我使用高级自定义字段并在转发器中使用转发器。我需要嵌套的转发器随机拉一行。以下是我的工作:

<?php $i = 0; while(the_repeater_field('squares')): ++$i;
        $repeater = get_sub_field( 'images' );
        $rand = rand(0, (count($repeater) - 1));?>
        <a href="<?php the_sub_field('link'); ?>" class="fs-square" style="background-image:url('<?php echo $rand['four-square']; ?>')">    
            <div class="main-text"><?php the_sub_field('main_text'); ?></div>
            <div class="icon-section">
                <div class="icon"><?php the_sub_field('icon'); ?></div>
                <div class="icon-text"><?php the_sub_field('icon_text'); ?></div>
            </div>
        </a>
    <?php endwhile; ?>

基本上,我有一个广场转发器,然后我有一个转发器用于图像。我需要图像转发器是随机的。

2 个答案:

答案 0 :(得分:0)

你可能想尝试

<?php echo $repeater[$rand]['four-square']; ?>

而不是

<?php echo $rand['four-square']; ?>

HTH!

答案 1 :(得分:0)

这就是为我解决的问题。

<?php 
    $i = 0; 
    while(have_rows('squares')): 
    the_row();
        $i++;
        $repeater = get_sub_field( 'images' );
        $rand = rand(0, (count($repeater) - 1)); //does not select a specific row but rather just a number
        ?>
        <a href="<?php the_sub_field('link'); ?>" class="fs-square" style="background-image:url('<?php echo $repeater[$rand]['image']['sizes']['four-square']; ?>')">   
            <div class="main-text"><?php the_sub_field('main_text'); ?></div>
            <div class="icon-section">
                <div class="icon"><?php the_sub_field('icon'); ?></div>
                <div class="icon-text"><?php the_sub_field('icon_text'); ?></div>
            </div>
        </a>
    <?php endwhile; ?>