It’s extremely popular to display adverts at the bottom of posts on a blog. The reader reads the blog posting, arrives at the end of the post, and the first thing he/she sees is an advert. If you have optimized your website correctly, the chances are that your advert will suite the topic just blogged about, and this will increase your chances of receiving a click from the user. What happens if you would like to rotate the adverts, so that you are not just displaying Google Adsense, for example, at the bottom of every post?

This is tricky, and I have not seen too many solutions around, so I decided to code my own solution to this problem. Below I will paste some of my code, along with the best description I can put together. I use PHP in this example.

Line    
1 <?php Starts a PHP segment
2 $num = rand(1,2); Randomly selects a 1 or a 2
3 if ($num == 1) If a 1 was selected
4 { then.
5 ?> (turn php off)
6 // advert code here paste your adsense code here
7 <?php (turn php on)
8 } finished with advert if the random number is 1
9 else if the random number was a 2
10 { then
11 ?> (turn off php)
12 // advert code here paste your adsense code here
13 <?php (turn php on)
14 } finish with advert if the random number is 2
15 ?> (turn off php)
     

Alright, now that the code is there, I can start explaining. It looks rather odd and tricky, but really it isn’t, especially if you have any knowledge of PHP. This little code snippet handles two different affiliate programs, and displays them randomly under your blog posts. I hope my explanation above was helpful, it is not easy explaining code so that everyone will follow.

Copy and paste the above code into notepad, and save the file as randomad.php. Once you have done this, all that is left, is placing it on your website, which will be done with this code:

<?php include(’randomad.php’); ?>

If you are a wordpress user, here are the steps to place this code correctly, so random adverts appear under each post:

1. Login to WP-Admin
2. Click Presentation
3. Click Theme Editor
4. Click Main Index Template, on the right of the screen
5. This is where things get tricky, now you need to look for the end of your posts code. Normally you would see something about comments or links to technorati or permalinks, you would place the code under that. Each theme is different so it is hard for me to tell you exactly where it goes. A common occurance is this..

</div>
<?php endwhile; endif; ?>

Which you should be able to find in the Main Index Template, if you see this then place the <?php include(’randomad.php’); ?> line of code right above the <.div>, like this..

<?php include(’randomad.php’);?>
</div>
<?php endwhile; endif; ?>

6. Save the changes by clicking “Update File”, and go back to your website and refresh, wallah.

If you have any questions about implementing this, please feel free to contact The Webtrepreneur! Best of luck!