Support

Account

Home Forums Add-ons Repeater Field Randomizing/Shuffling Repeater with Advanced Loop

Solved

Randomizing/Shuffling Repeater with Advanced Loop

  • Hi there. I am trying to shuffle the output/order of a repeater field. Everything I’m finding about randomizing/shuffling all utilize the basic loop. I’m using the advanced and would prefer to utilize that for things like outputting the array of classes.

    <?php if( have_rows('clients') ): ?>
    	
    	<div class="outerContainer clientGrid clearfix">
    
    	<?php while( have_rows('clients') ): the_row(); 
    
    		// vars
    		$name = get_sub_field('client_name');
    		$img = get_sub_field('logo');
    		$docVid = get_sub_field('doc_or_vid');
    		$docURL = get_sub_field('document_location');
    		$vidURL = get_sub_field('video_url');
    
    		?>
    
    		<div class="clientItem clearfix <?php the_sub_field('areas'); ?>">
    
    			<?php if($docURL && $docVid == 'doc'): ?>
    				<a target="_blank" class="clientLink" href="<?php echo $docURL; ?>">
    					<span class="sprite"></span>
    				</a>
    			<?php endif; ?>
    
    			<?php if($vidURL && $docVid == 'vid'): ?>
    				<a target="_blank" class="clientLink clientVid external" href="<?php echo $vidURL; ?>">
    					<span class="sprite"></span>
    				</a>
    			<?php endif; ?>
    			
    			<?php if( $img ): ?>
    				<img class="client-logo" src="<?php echo $img; ?>" alt="<?php echo $name; ?>" />
    			<?php endif; ?>
    
    		</div>
    
    	<?php endwhile; ?>
    
    	</div><!-- ./clientGrid -->
    
    <?php endif; ?>
  • Hi @rkeefer

    I believe you can use the acf/load_value hook to shuffle the returned data like this:

    function my_acf_load_value3( $value, $post_id, $field )
    {
        shuffle($value);
        return $value;
    }
    add_filter('acf/load_value/name=repeater_field_name', 'my_acf_load_value3', 10, 3);

    Where “repeater_field_name” is the name of your repeater.

    I hope this helps 🙂

  • So should it be as simple as inserting it like this:

    <?php if( have_rows('clients') ): ?>
    
    	<?php
    		function my_acf_load_value3( $value, $post_id, $field )
    		{
    			shuffle($value);
    			return $value;
    		}
    		add_filter('acf/load_value/name=clients', 'my_acf_load_value3', 10, 3);
    	?>
    	
    	<div class="outerContainer clientGrid clearfix">
    
    	<?php while( have_rows('clients') ): the_row(); 
    
    		// vars
    		$name = get_sub_field('client_name');
    		$img = get_sub_field('logo');
    		$docVid = get_sub_field('doc_or_vid');
    		$docURL = get_sub_field('document_location');
    		$vidURL = get_sub_field('video_url');
    
    		?>
    
    		<div class="clientItem clearfix <?php the_sub_field('areas'); ?>">
    
    			<?php if($docURL && $docVid == 'doc'): ?>
    				<a target="_blank" class="clientLink" href="<?php echo $docURL; ?>">
    					<span class="sprite"></span>
    				</a>
    			<?php endif; ?>
    
    			<?php if($vidURL && $docVid == 'vid'): ?>
    				<a target="_blank" class="clientLink clientVid external" href="<?php echo $vidURL; ?>">
    					<span class="sprite"></span>
    				</a>
    			<?php endif; ?>
    			
    			<?php if( $img ): ?>
    				<img class="client-logo" src="<?php echo $img; ?>" alt="<?php echo $name; ?>" />
    			<?php endif; ?>
    
    		</div>
    
    	<?php endwhile; ?>
    
    	</div><!-- ./clientGrid -->
    
    <?php endif; ?>
  • Think I figured it out. Rather than put it in the actual page template, it goes in the functions.php:

    // Function to randomize Advanced Custome Fields' Repeaters
    function my_acf_load_value3( $value, $post_id, $field )
    {
    	shuffle($value);
    	return $value;
    }
    
    // Randomize ACF Clients' Repeater
    add_filter('acf/load_value/name=clients', 'my_acf_load_value3', 10, 3);
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Randomizing/Shuffling Repeater with Advanced Loop’ is closed to new replies.