如何随机化URL列表';s在PHP中使用ACF


How to randomise a list of URL's in PHP using ACF?

我有一个网页,它使用ACF Repeater字段从wordpress调用7个图像。

我想做的是获得图像的URL列表,然后对它们进行洗牌,使它们的图像随机出现在网页上。当我用($image['url'])调用图像时,唯一显示的图像是上传到wordpress网站的最后一张图像。

<?php       
    // Call the ACF Gallery images 
    $imageArray = get_field('company_logos', 13);
    $size = 'full'; // (thumbnail, medium, large, full or custom size)
    if( $imageArray ):
        while( have_rows('company_logos', 13) ) : the_row();
             $image = get_sub_field('image');
            // $content = get_sub_field('content');
              //shuffle ($image['url']);
              $arr = $image['url'];
              shuffle($arr);
              print_r($arr);
        endwhile;
        endif; ?>

当我在屏幕上回显图像URL时,它们的格式为:

http://localhost/wordpress/wp-content/uploads/2016/07/a.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/b.png
http://localhost/wordpress/wp-content/uploads/2016/07/c.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/d.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/e.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/f.jpg
http://localhost/wordpress/wp-content/uploads/2016/07/g.jpg

有人知道怎么做吗?谢谢

您从$image['url']中获得图像列表吗?它会首先返回一个数组吗?如果确实如此,那么您的解决方案应该有效。如果它不是一个数组,而是一个用逗号分隔的URL字符串,那么在第二条语句之前执行以下操作。因此,新代码看起来如下,

$urls = $image['url'];
$arr = explode(" ", $urls);
shuffle($arr);
print_r($arr);