Support

Account

Home Forums Add-ons Repeater Field Random image repeater

Solving

Random image repeater

  • Hi, I’m trying to create a random repeater using this method, but the output is blank. Can anyone spot where I’m going wrong?

    <?php 
    
    	$rows = get_field('homepage_images' ); // get all the rows
    	$rand_row = $rows[ array_rand( $rows ) ]; // get a random row
    	$rand_row_image = $rand_row['image_set']; // get the sub field value 
    
    	$image = wp_get_attachment_image_src( $rand_row_image, 'full' );
    	// url = $image[0];
    	// width = $image[1];
    	// height = $image[2];
    ?>
    
       <header class="hero" style="background-image: url(<?php echo $image[0]; ?>)">
    
       </header>
  • you’re using array_rand() wrong

    
    $rand_row = array_rand($rows, 1); // get a random row
    

    http://php.net/manual/en/function.array-rand.php

  • Thanks I got it sorted – I was using URL as the image type. Changed it to ID and it worked.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Random image repeater’ is closed to new replies.