Support

Account

Home Forums Add-ons Repeater Field Define number of rows repeater field fetches back and randomise which it pulls

Helping

Define number of rows repeater field fetches back and randomise which it pulls

    • dubbs

    • October 13, 2019 at 8:05 am

    Hi,

    I am pulling in some ACF data from a defined page in my template – this works as per code below, what I would like to do is define the number of ‘results’ displayed and pull them in, in a random format – so lets say their are 10 copies of the repeater field ‘testimonialREPEATER’ on page id 1480. Id like the code below to pull all the info it does – BUT to just pull a random 3 of the 10, so that only 3 rather than all 10 are displayed… Is this possible?

    <?php
    
    			// check if the repeater field has rows of data
    			if( have_rows('testimonialREPEATER', 1480) ):
    			
    				// loop through the rows of data
    				while ( have_rows('testimonialREPEATER', 1480) ) : the_row();
    			
    			?>
    			
                        <!-- v2 -->
    					<div class="testimonialItemWrapper">
                        <div class="testieImageFocus<?php the_sub_field('testimonialIMAGEFOCUS'); ?> testimonialItemQUOTEWrapper">
                        <?php the_sub_field('testimonialQUOTE'); ?>
                        <h3><?php the_sub_field('testimonialAUTHOR'); ?><?php if( get_sub_field('testimonialPOSITION') ): ?> - <?php the_sub_field('testimonialPOSITION'); ?><?php endif; ?><?php if( get_sub_field('testimonialCOMPANY') ): ?>, <?php the_sub_field('testimonialCOMPANY'); ?><?php endif; ?>
                        </h3>
                        </div>
                        <div class="testieImageFocus<?php the_sub_field('testimonialIMAGEFOCUS'); ?> testimonialItemIMGWrapper" style="background-image:url('<?php the_sub_field('testimonialIMG'); ?>');">
                        <!--<img src="<?php the_sub_field('testimonialIMG'); ?>" title="<?php the_sub_field('testimonialIMG'); ?>" alt="<?php the_sub_field('testimonialIMG'); ?>"/>-->
                        </div>
                        </div>
                        
    			
                <?php
    				endwhile;
    			
    			else :
    			
    				// no rows found
    			
    			endif;
    			
    			?>
  • I would do it a different way and not use an acf have_rows() loop

    
    $repeater = get_field('testimonialREPEATER', 1480);
    $indexes = array_rand($repeater, 3);
    foreach ($indexes as $index) {
      var_dump($repeater[$index]);
    }
    

    Using the above, instead of using the_sub_field() you would need to access the value in the array, for example testimonialQUOTE would be

    
    echo $repeater[$index]['testimonialQUOTE'];
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.