0
votes

I'm using Wordpress Advanced Custom Fields plugin and trying to output a random image in a repeater field. I got so far using ACF documentation, but their code seems to not work, as the $image[0] variable is empty. Help ? :)

<?php 

$rows = get_field('les_gifs' ); // get all the rows
$rand_row = $rows[ array_rand( $rows ) ]; // get a random row
$rand_row_image = $rand_row['gif' ]; // get the sub field value 

// Note
// $first_row_image = 123 (image ID)

$image = wp_get_attachment_image_src( $rand_row_image, 'large' );
// url = $image[0];
// width = $image[1];
// height = $image[2];

echo 'image 0 : '.$image;

?>

<?php if($image[0]) : 
    $style= 'style="background-image:linear-gradient(135deg, rgba(0,89,167,0.65) 0%,rgba(23,208,233,0.65) 100%),url('.$image.');"';
 else: 
    $style = '';
 endif; ?>
1

1 Answers

1
votes

Turns out I made a rookie mistake. The code works fine. I'm calling this outside of the Loop and I forgot to add the post id.

$rows = get_field('les_gifs', 432 );

this bit solved it