How to get rotate.php to show multiple images on same page

Using rotate.php to display random images is super simple, but what if you want to display multiple random images on the same page? With the default configuration the same image is loaded in all instances on the one page. When the page is refreshed all the images change to another random image, but a little redundant, no?

To get multiple random images from the same folder, add a counter and echo the counter within the call to rotate.php, as exemplified in this simplified WordPress loop:

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); $counter++; ?>

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

<div class="thumb">
<?php $counter++;?><img src="path/to/your/rotate.php?<?php echo $counter;?>" />
</div>

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>

</div>

<?php endwhile; ?>

<?php endif; ?>

 

Notice $counter++. This tells the counter to increase for each image. Then echoing the counter after ../rotate.php makes it soe each image has a unique string (../rotate.php?1 ../rotate.php?2 etc.)

If you are not familiar with rotate.php, grab the file below and stick it in a folder where you will house your random images. Let’s say you have a random_images folder on the root of your site where you want to store your random images. Then your path would be http://yoursite.com/random_images/rotate.php?<?php echo $counter; ?>

Leave a Reply




Pingbacks