WordPress + ACF gallery 图像大小的随机数组

WordPress + ACF gallery random array of image sizes

我正在使用 ACF 和 Isotope Masonry 构建图像库,但我希望图像以不同的大小随机显示。

我使用的是当前代码,但它不能正常工作,请问有人能指出我正确的方向吗?

 <?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; ?>

谢谢 CBroe,我已经更新到下面,但它仍然无法正常工作,这是正确的吗?

<?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; ?>

您正在获取密钥,但您传递的是密钥而不是传递该密钥的值。我已经更新了您的代码,请检查。

<?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; ?>