Codeable info

Simple PHP Snippet That Allows You To Rotate Banners Or Text

Posted on by in WordPress Tutorials

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 (“).

$banner_rotate[0]="<a href=''><img src='' /></a>";
$banner_rotate[1]="<a href=''><img src='' /></a>";
$banner_rotate[2]="<a href=''><img src='' /></a>";
$banner_rotate[3]="<a href=''><img src='' /></a>";
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.


Codeable info

Comments (5)

Comment by Nick Plekhanov says:

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

Comment by Kevin Muldoon says:

Good tip Nick. Thanks for sharing :)

Comment by Nick Plekhanov says:

$banner_rotate_size = count( $banner_rotate );

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

and this:

$banner_rotate[] = 'list';
$banner_rotate[] = 'list';
$banner_rotate[] = 'list';
$banner_rotate[] = 'list';
// much much more items …..

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

Good tip though.

Comment by Kevin Muldoon says:

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.

Comment by George Serradinho says:

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.

Codeable info