【问题标题】:Random images with acf (gallery option)带有 acf 的随机图像(图库选项)
【发布时间】:2021-08-23 05:21:07
【问题描述】:

你能告诉我我需要添加什么来制作随机图片库吗? 我需要创建像 $rand 这样的新变量吗? 我正在寻找答案,但我找不到。我创建了 $rand = array_rand($gallery, 1);。但它不起作用。帮助:)

<section class="gallery-section">
    <div class="container padding">
        <h2>Galeria</h2>
        <div class="gallery-container">
            <div class="row">
                    <?php 
                $images = get_field('gallery');
                
                if( $images ): ?>
                 <?php foreach( $images as $image ): ?>
                <div class="col-lg-4 col-md-6">
                    
                    <div class="image-wrapper">
                        <a class=“fancybox” rel=“gallery href="<?php echo esc_url($image['url']); ?>">
                     <img class ='img-fluid'  src="<?php echo esc_url($image ['sizes']['large']); ?>" alt="<?php echo esc_attr($image['alt']); ?>"  />
                    </a>
                    
                    </div>
                    
                </div>
                <?php endforeach; ?>
                    <?php endif; ?>
                
                
            </div>

        </div>
    </div>
</section>

【问题讨论】:

    标签: php html wordpress advanced-custom-fields gallery


    【解决方案1】:

    array_rand() 用于从给定数组中选择一个(或多个)随机键。如果您想以随机顺序呈现画廊的图像,您应该使用 shuffle() 代替:

    $images = get_field('gallery');
    if ($images) {
        shuffle($images); // <-- Note that it modifies to original array.
        foreach($images as $image) {...}
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-10
      相关资源
      最近更新 更多