Support

Account

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

Solved

Random Gallery Image with wp_get_attachment_image()

  • I’ve found others using the following code for displaying a random gallery image (and it works):

    <?php
    $gallery = get_field('images');
    $rand = array_rand($gallery, 1);
    
    if( $gallery ): ?>
    	<img src="<?php echo $gallery[$rand]['sizes']['large']; ?>" alt="<?php echo $gallery[$rand]['alt']; ?>" />
    <?php endif; ?>

    But I’m trying to do it with wp_get_attachment_image() (for responsive images) but not sure how to go about it? Any help would be greatly appreciated.

  • Hey @codeview

    Have a look at the Gallery field documentation here https://www.advancedcustomfields.com/resources/gallery/

    Under section “Basic list of images” you will find how to use the wp_get_attachment_image() function

    Panos

  • Yes, sorry, perhaps I should have been more clear. I’m not sure how to make the $rand variable apply. I tried things like this, to no avail:

    
    <?php 
    	$images = get_field('gallery', 'option');
    	$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; ?>
    
  • Cool, I see the problem with your code; you’re not passing the correct arguments to wp_get_attachment_image. It expects as a first argument an attachment ID. In your case, you’re passing the whole image array.

    So instead of $images[$rand] you should get the ID out of this array.

    
    wp_get_attachment_image( $images[$rand]['ID'], $size );
    

    Panos

  • Another option is to get the unformulated value from ACF. Since you’re going to generate the image yourself using wp_get_attachment_image(), there really is not point in having ACF do all the queries involved to get all that image data that you’re not going to use.

    
    $images = get_field('gallery', 'option', false);
    

    will return an array of image IDs

  • 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; ?>
  • Thank you. I marked this as the solution before seeing the answer from @hube2. His solution made my code work (as I did not know about the false parameter, and the added benefit of not needing ACF to do additional queries) but your solution worked as well.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.