Support

Account

Home Forums Add-ons Repeater Field Random image from repeater

Helping

Random image from repeater

  • Hi everybody,

    I need help…

    I’ve got a ‘slideshow-image’ image field (image object) under a ‘slideshow’ repeater.

    The problem is that I’d like to display only one random picture at a time, in ‘large’ format.

    I get every pictures in a $images string.

    $images = get_sub_field('slideshow_image');

    I know I can get the image URL in the right format like this :

    $sizes = $images['sizes']['large'];

    But then I don’t know how to get only 1 random image url in the right format.

    Please could you help me ?
    Thanks a lot !

  • This code is from the Repeater Field documentation and should do what you want:

    <?php 
    
    $rows = get_field('slideshow'); // get all the rows
    $rand_row = $rows[ array_rand( $rows ) ]; // get a random row
    $rand_row_image = $rand_row['slideshow-image']; // get the sub field value 
    
    $image = wp_get_attachment_image_src( $rand_row_image, 'large' );
    // url = $image[0];
    // width = $image[1];
    // height = $image[2];
    ?>
    <img src="<?php echo $image[0]; ?>" />
Viewing 2 posts - 1 through 2 (of 2 total)

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