Support

Account

Home Forums Add-ons Gallery Field Random Gallery Image with wp_get_attachment_image() Reply To: Random Gallery Image with wp_get_attachment_image()

  • Ah, interesting. I didn’t know about unformulated values and the false parameter. Thank you.

    For anyone seeing/needing this later, here is the final working code (just remove 'option', if you’re not using a gallery from an Options page):

    <?php 
    	$images = get_field('gallery', 'option', false);
    	$size = 'full'; // (thumbnail, medium, large, full or custom size)
    	$rand = array_rand($images, 1);
    
    	if( $images ): ?>
    		<?php echo wp_get_attachment_image( $images[$rand], $size ); ?>
    <?php endif; ?>