Revision 429 is a pre-publication revision. (Viewing current revision instead.)

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: [php]$rand_max = count(get_field('testimonials'));[/php] 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:   [php] <?php $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> [/php]

Revisions

  • May 2, 2013 @ 11:17:49 [Current Revision] by PeterLugg
  • May 2, 2013 @ 11:17:16 by PeterLugg

Revision Differences

May 2, 2013 @ 11:17:16Current Revision
Content
Unchanged: First you need to store a variable with a random number no greater than the amount of rows in the repeater:Unchanged: First you need to store a variable with a random number no greater than the amount of rows in the repeater:
Unchanged: [php]$rand_max = count(get_field( 'testimonials'));[/php]Unchanged: [php]$rand_max = count(get_field( 'testimonials'));[/php]
Unchanged: 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:Unchanged: 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:
 Added: &nbsp;
 Added: [php]
 Added: &lt;?php
 Added: $rand_max = count(get_field( 'testimonials'));
 Added: $i = 0;
 Added: ?&gt;
 Added: &lt;?php while(the_repeater_ field('testimonials')): ?&gt;
 Added: &lt;?php if($rand_max == $i): ?&gt;
 Added: &lt;em id=&quot;__mceDel&quot;&gt;
 Added: &lt;?php the_sub_field('name'); ?&gt;
 Added: ...
 Added: &lt;?php endif; ?&gt;
 Added: &lt;?php $i++; ?&gt;
 Added: &lt;?php endwhile; ?&gt;&lt;/em&gt;
Deleted: [php][/php] Added: [/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply