Advanced Custom Fields – Display a random repeater field

First you need to store a variable with a random number no greater than the amount of rows in the repeater:

$rand_max = count(get_field('testimonials'));

Then you need to loop through the repeater rows with an incrementing key and if the key equals the random number, then display the row:


    $rand_max = count(get_field('testimonials'));
    $i = 0;

<?php while(the_repeater_field('testimonials')): ?>

    <?php if($rand_max == $i): ?>
<em id="__mceDel">
 <?php the_sub_field('name'); ?>

 <?php endif; ?>

 <?php $i++; ?>

<?php endwhile; ?></em>


No comments yet.

Leave a Reply