如何在PHP中显示两个不同的随机图像?

man*_*anu 1 php wordpress

我需要显示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>
Run Code Online (Sandbox Code Playgroud)

Dav*_*ave 5

试试这个:

$imagesDir = 'images/tips/';

$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);

$randomImage = $images[array_rand($images)]; // See comments
Run Code Online (Sandbox Code Playgroud)

您可以向array_rand()发送第二个参数以获得多于1个.

另外看看这些链接:

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

php从目录生成随机图像

PHP从文件夹中提取随机图像