簡體   English   中英

WordPress + ACF 畫廊圖像大小的隨機數組

[英]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; ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM