Skip to content

Instantly share code, notes, and snippets.

@tjhole
Last active January 26, 2020 02:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjhole/8054985 to your computer and use it in GitHub Desktop.
Save tjhole/8054985 to your computer and use it in GitHub Desktop.
ACF: Random image from Gallery
@jared-rice
Copy link

jared-rice commented Jul 5, 2019

This worked great for me, thanks!

Heres a background image conversion if anyone is interested:

<?php
     $gallery = get_field('images');
     $rand = array_rand($gallery, 1);

     if( $gallery ):
           $background_image_full = wp_get_attachment_image_src( $gallery[$rand]['ID'], 'full'); ?>
	   <div style="background-image: url('<?php echo esc_url($background_image_full[0]); ?>')">
<?php endif; ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment