Rotate Banner Ads In WordPress Without Using Plugins

PHP knowledge can be useful in replicating some of the wordpress plugins with simple php code without the need to add to the list of long plugins one uses on a wordpress blog.Here’s a simple piece of PHP code which lets rotate banners, more specifically for a 125 x 125 ad slots.

<?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];
}
?>

elePHPant

What this piece of code does is, it stores the ad code in the array with easy banner code stored in a array ie., $banner_rotate [i] where i can be 0 to n ( any number).

Next the shuffle function rotates the contents of all array elements, before looping to select any of the banner.

Next starts the loop which displays all the banners.

Source – WPMods

If your not comfortable with placing php code into your template files, you can try these plugins :

Also Checkout, How To Place Ads In The Middle Of The Post Automatically

Here’s another way to rotate ads., This lets you show a randon ad eveytime a page is refreshed

<?php $rotads = rand(0,1);?>

<?php if ($rotads == 0) {?>

<!—-Place First Banner Code Here –>

<?php if ($rotads == 1) {?>

<!—-Place Second Banner Code Here –>

<?php };?>
This can also be used to do A/B Testing by placing different ads and testing which ad is giving the best results.

The HostGator Ads In the Sidebar on this blog, is implemented using the same above mentioned method.

Similar Post  Link To Old Posts Easily With RB Internal Links

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.