Simple PHP Snippet That Allows You To Rotate Banners Or Text

Written by on June 25, 2011 in WordPress Tutorials - 5 Comments

When I placed the exclusive WordPress Theme Discount banners on the WP Mods sidebar I used some basic PHP in order to shuffle the banners around. This ensured that all banners got equal exposure.

I’d like to share with you all today the simple code I used to achieve this banner rotation. The code will not track impressions or click throughs; all it does is ensure that all entries in the array are shuffled every time the page is loaded. You can place any code within each array (e.g. PHP, CSS, HTML etc).

In the example below I have called the array string $banner_rotate. You could change this to anything you want e.g. $textshuffle, $banshuff etc. Also, make sure that you use the single quotation mark (‘) within the array instead of double quotations (“).

<?php 
 
$banner_rotate[0]="<a href='http://www.site.com'><img src='http://www.site.com/banner0.png' /></a>";
 
$banner_rotate[1]="<a href='http://www.site.com'><img src='http://www.site.com/banner1.png' /></a>";
 
$banner_rotate[2]="<a href='http://www.site.com'><img src='http://www.site.com/banner2.png' /></a>";
 
$banner_rotate[3]="<a href='http://www.site.com'><img src='http://www.site.com/banner3.png' /></a>";
 
shuffle($banner_rotate);
 
for ($i=0;$i<=3;$i++){
   echo $banner_rotate[$i];
}
 
?>

The shuffle function mixes the array around and the final piece of code ensures that all entries in the array are displayed on your site. Adding more entries into the array is simple; you just need to make sure you increment $banner_rotate[3] to $banner_rotate[4] and then $banner_rotate[5] and so on. Remember to edit the for statement at the end to reflect the number of entries in the array (e.g. change $i<=3 to $i<=5 etc).

If you are unsure about any aspect of this, please let me know.

Thanks,
Kevin

About

Kevin Muldoon is a Scottish webmaster and blogger who currently lives in Bogota, Colombia. He has an unhealthy obsession with trying out new WordPress themes and plugins and spends too much time in the WordPress Forums.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

this:
$banner_rotate_size = count( $banner_rotate );

for ( $i = 0; $i &lt; $banner_rotate_size; $i++ ) {
// your code goes here
}

and this:

$banner_rotate[] = &#039;list&#039;;
$banner_rotate[] = &#039;list&#039;;
$banner_rotate[] = &#039;list&#039;;
$banner_rotate[] = &#039;list&#039;;
// much much more items .....

would be more universal and more clear and more easier to maintain.

Good tip though.

Good tip Nick. Thanks for sharing :)

Keep up great work!. Always read your articles/tuts. Thanks for good work.

I have also used this method in the past and it works fine. It's a simple and easy way of rotating banners without using a WP plugin.

I would suggest maybe using a div around the banners block for spacing and borders, etc. Using CSS is simple and also easy to implement.

Good tip. Yeah you can easily style the area using CSS. I didn't need to use CSS with the banners I was using as the banners were the same width as the sidebar.