How to display a random item?

36 views
Skip to first unread message

colosimo

unread,
Jul 20, 2010, 11:33:40 AM7/20/10
to Magic Fields
Hello.
I want to upload 4 images to my post and then i want just one image to
be displayed randomly every time you get on the page.

I've got something like that which display everything:

<?php $total = getFieldDuplicates('image', 1);?>
<?php for($i = 1; $i < $total+1; $i++):?>
<?php $imagenum = get('image',1,$i);
echo do_shortcode('[showimage id='.$imagenum .']'); ?>
<?php endfor;?>
<?php } ?>

I'm searching from the missing link, pick just one ranfom $imagenum
and echo just that one.
I'm not a real PHP programmer, but i believe this is pretty easy for
you guys.
Thank you for your help!

c.

Hunk

unread,
Jul 20, 2010, 12:35:25 PM7/20/10
to Magic Fields
<?php

$num_images = count( getFieldOrder('image') );
$rand_image = rand(5, $num_images);

$imagenum = get('image',1,$rand_image);
echo do_shortcode('[showimage id='.$imagenum .']');

?>

colosimo

unread,
Jul 20, 2010, 12:57:18 PM7/20/10
to Magic Fields
Thank you very much Hunk!
Does it work even if the number of images is different from 4 or do I
need something different?

c.
Reply all
Reply to author
Forward
0 new messages