【问题标题】:How to show two different random images in PHP?如何在 PHP 中显示两个不同的随机图像?
【发布时间】:2016-02-26 23:21:12
【问题描述】:

我需要显示 2 个不同的随机图像。 我尝试了此代码,但第二张图片显示了与第一张相同的图片。 我必须复制这个但有 15 个随机图像。 http://www.kometschuh.de/Example-Random-CSS3-Image-Flip-Effect.html 对于每个<li>,我需要显示 2 个不同的随机图像

<ul class="flip">
  <?php
    $all_images = glob("wp-content/themes/connexia/img-test/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);

    shuffle($all_images);

    foreach ($all_images as $index => $image ) {
     if ($index == 15) break;  // Only print 15 images
     $image_name = basename($image);
     $image_name2 = basename($image++);
     echo "<li>
               <img src='/wp-content/themes/connexia/img-test/{$image_name}' />
               <img src='/wp-content/themes/connexia/img-test/{$image_name2}' />
          </li>";
    }
  ?>
</ul>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    试试这个:

    $imagesDir = 'images/tips/';
    
    $images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $randomImage = $images[array_rand($images)]; // See comments
    

    您可以向 array_rand() 发送第二个参数以获得大于 1 的值。

    也看看这些链接:

    http://askwebexpert.com/tutorials/how-to-display-random-images-from-a-directory-using-php/

    php generate random image from a directory

    PHP pull random image from folder

    【讨论】:

  • 你仍然可以使用这个,每次
  • 迭代请求 2 个随机图像
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签