0
votes

How to show x number image random in acf

i have add code number number image random in acf don't show image

<?php 
$images = get_field('khach_hang', 'option');
$rand = array_rand($images, 1);
if( $images ): ?>
<?php 
$i = 0;
foreach( $images as $image ): 
if ($i <= 19 ) { ?>
        <img class="khac_hang_item" src="<?php echo $image[$rand]['url']; ?>" alt="<?php echo $image[$rand]['alt']; ?>" />
<?php $i++; }  endforeach; ?>
<?php endif; ?>

I have 20 url image : <img class="khac_hang_item" src="" alt="" />

Any help greatly, Thanks

1

1 Answers

0
votes

$rand will be an array of keys from $images..

I guess you should rewrite your code to something like this:

<?php 
$images = get_field('khach_hang', 'option');
if( $images ):
$rand_keys = array_rand($images, 10);
foreach( $rand_keys as $key ): ?>
    <img class="khac_hang_item" src="<?php echo $images[$key]['url']; ?>" alt="<?php echo $images[$key]['alt']; ?>" />
<?php  endforeach; ?>
<?php endif; ?>

you should loop $rand_keys instead of $images...