How to Insert Random Affiliate Banners
tagged Affiliates, How to Insert Random Affiliate Banners and Wordpress
There are many affiliates that require you to use banners (468×60) which don’t fit on a sidebar. Affiliates such as text-link ads, blog mastermind and amazon all provide these banners that you can display on your pages. If you are an affiliate for several sites like me, then the last thing you want to do is to stick banners all over your home page - this can be a real turn off as it gives people the impression you are just advertising and it can make your site look too spammy.
I found the solution and it’s easy! You can add a random affiliate banner anywhere on your site by simply writing some code into your template file. I’m going to show you how to insert random affiliate banners on all your pages just above the posts. To do this you will need to edit the template files index.php, archive.php, page.php and single.php. You can display as many affiliate banners as you want to. To see a demo of this you can see the way I have done it on this site.
Here is the code and step by step instructions on how an where to inset it. Good Luck!
- Login to your WordPress administration panel
- Select Presentation ->Theme Editor from the navigation menu
- On the Theme Editor page, you will see a list of “theme files” to the right of the page. Select the “main index template” (index.php).
- Find the following line
<div id=”primaryContent”> - Below it copy and paste the following code
<?php$show = rand (0,3);switch ($show) {
case 0:
?>
<a href=”1st affiliate url goes here”></a><?php
break;
case 1:
?>
<a href=”2nd affiliate url goes here></a>
<?php
break;
case 2:
?>
<a href=”3rd affiliate url goes here”></a><?php
break;
case 3:}
?>
- Save
You can view your site now and you should be able to see 3 random affiliate banners on your home page just above the posts.
You need to now repeat the above steps 1- 6 on the following template files:
archive.php - this will show the banners on all your category pages
page.php - this will show the banners on all your pages you create in Wordpress
single.php - this will show the banners on every single post
That’s it your done ![]()

