Home Forums Add-ons Repeater Field Randomizing Repeater Rows


Randomizing Repeater Rows

  • I’ve created a carousel widget that takes repeater field data and outputs it. The data is an image and URL. It’s working fine, and outputting in the order it’s added, but I’m wondering if there’s a way to output it randomly? Any help greatly appreciated. Pertinent code is below.

    public function widget( $args, $instance ) {    
    				if	(!get_field('logo_carousel')) { return; }
    				$title = apply_filters( 'widget_title', $instance['title'] );
    				$this->count = isset( $instance[ 'count' ] ) ? $instance[ 'count' ] : 4;  
    				echo $args['before_widget'];  ?>
    				<h3><?php print $title; ?></h3>
    				<div class="logo-carousel-items">
    						<?php	while	(has_sub_field('logo_carousel')):	?>
    										$image_id	=	get_sub_field('image');						
    										$link	=	get_sub_field('link');												
    										$image_size = 'gsa-carousel-link';
    										<a href="<?php	echo	$link	?>" target="_blank">								
    												<?php	echo wp_get_attachment_image( $image_id, 175,125 );	?>
    						<?php	endwhile;	?>
  • primary it should work with help of php function shuffle.
    but you need to rebuild your code and replace the while loop with a foreach loop to use it.

    here is a simple code how to use randomize(shuffle):

    $rows = get_field('repeater_field_name' ); //get all rows
    shuffle ($rows); //randomize rows
    if($rows) {
    	foreach($rows as $row) { //print rows
    		echo $row['sub_field_name'];
    		echo ' ';
    		echo $row['sub_field2_name'];
    		echo '<br>';
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Randomizing Repeater Rows’ is closed to new replies.