Paddy Winsley
Paddy Winsley

Reputation: 67

WordPress + ACF gallery random array of image sizes

I'm building a gallery of images with ACF and Isotope Masonry, but I'd like the images to be displayed at different sizes randomly.

I'm using the current code which isn't quite working, can anyone point me in the right direction as to what's going wrong please?

 <?php 
$images = get_sub_field('gallery');
$size = array("gallery-large","gallery-medium","gallery-small"); // (thumbnail, medium, large, full or custom size)
$rand = array_rand($size, 1);

if( $images ): ?>
    <div class="gallery grid">

        <?php foreach( $images as $image_id ): ?>
  <div class="grid-item">
                <?php echo wp_get_attachment_image( $image_id, $rand ); ?>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

Thanks CBroe, I've updated to the below and it's still not working, is this correct?

<?php 
$images = get_sub_field('gallery');

if( $images ): ?>
    <div class="gallery grid">

    <?php foreach( $images as $image_id ):
    $size = array("gallery-large","gallery-medium","gallery-small");
    $rand = array_rand($size, 1); ?>

        <div class="grid-item">
            <?php echo wp_get_attachment_image( $image_id, $rand ); ?>
        </div>

        <?php endforeach; ?>
    </div>
<?php endif; ?>

Upvotes: 0

Views: 467

Answers (1)

Brijesh Dhanani
Brijesh Dhanani

Reputation: 886

You are getting key but you are passing key instead of passing value of that key. I have updated your code please check.

<?php 
$images = get_sub_field('gallery');

if( $images ): ?>
    <div class="gallery grid">

    <?php foreach( $images as $image_id ):
    $size = array("gallery-large","gallery-medium","gallery-small");
    $rand = array_rand($size, 1); ?>

    <div class="grid-item">
        <?php echo wp_get_attachment_image( $image_id, $size[$rand] ); ?>
    </div>

    <?php endforeach; ?>
</div>
<?php endif; ?>

Upvotes: 1

Related Questions