A Dead Simple Way To Rotate Ads
You’ll notice (please!) that I have a grid of four square ads on the top of my right-hand sidebar. I wanted to find a way of randomly rotating a number of different ads into those four positions. I don’t need anything fancy beyond that, and none of the WordPress plugins that I found really worked for me, so I decided to hack something up myself.
I’m using LinkShare.com to deliver these ads, but the following approach should work for any ad source.
First, let’s take a look at what my sidebar.php template looked like before. This code snippet shows where I had four ads hardcoded into specific positions. I’m calling the links ad1.html through ad4.html and the images img1.png through img4.png just as an example:
So to replace those four static ads with, say, six ads that rotate randomly through the four positions, I just replaced the above code with the following:
And that’s it! Each time your sidebar is loaded it will randomly display four of your six ads in different positions each time. On my site I use WP Super Cache to improve performance, so you won’t actually see a different set of ads on the same page, but if you click to another page you’ll notice that the ads have refreshed.

I could hug you right now. I've been trying to figure out how to do this for hours. Thanks a ton for posting this. To those who want to display only 1 and not 4 at a time change ($ads, 0, 4) to ($ads, 0, 1).
Works like a charm
I could hug you right now. I've been trying to figure out how to do this for hours. Thanks a ton for posting this. To those who want to display only 1 and not 4 at a time change ($ads, 0, 4) to ($ads, 0, 1).
Works like a charm
This didn't work in Tumblr, just displayed several ads and showed the last bit of code as text. Do do with PHP?
Yes, you need to be able to execute PHP for this to work.
Yes, you need to be able to execute PHP for this to work.
Any clues on how to do this in a Tumblr?
Any clues on how to do this in a Tumblr?