Home Forums ACF PRO Random image from Repeater issue


Random image from Repeater issue

  • Hello.
    I am building out a repeater that calls an image.
    The image is to be random on each page reload.
    As reference I started with this code snippet/example:

    Here is a screenshot of my Repeater:

    Here is my code:

      $rows = get_field('jdh_homepage_images' ); // get all the rows
      $rand_row = $rows[ array_rand( $rows ) ]; // get the first row
      $rand_row_image = $rand_row['jdh_homepage_image']; // get the sub field value
      $image = wp_get_attachment_image_src( $rand_row_image, 'full' );

    This line did not work:
    <?php echo $image[0]; ?>

    But this line did:
    <?php echo $rand_row_image; ?>

    Thank you for any help with this issue.

  • What are you returning from the image field? Array, URL or ID

    If you’re returning the URL then you don’t need to do the last line wp_get_attachment_image_src. You only need to do that if you’re returning the ID.

  • @hube2 So I can remove $image = wp_get_attachment_image_src( $rand_row_image, 'full' ); and the should still work?

  • Yes, if you’re returning the url then $rand_row_image; already has what you want.

  • @hube2 Thank you for your response. I will try this and report back.

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

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