How to Optimise : Time based Adsense
August 30, 2007 | Web Design
In my Adsense tips post the other day I mentioned time based ads, and how I like to display ads based on the age of the article they are on - I should probably point out that I only use this technique on single posts, and not on the site at large.
I really like this idea for a number of reasons:
- It stops regular visitors from being snowed under with ads
- It means I don’t have to see the adverts
- It gives search engine visitors (who will likely end up at older posts) the advertising - and I feel they are more likely to click adverts
So how do I do this? It’s actually incredibly simple indeed (I hope). All I do is use the Wordpress command get_the_time(’U') to get the time the article was made. I then calculate what the time was x amount of days ago and compare that with the age of the article (to work out what age bracket the article falls in). I then place this code inside my “wordpress loop”.
<?php
// get the time of the current post
$u_time = get_the_time('U');
// reset my selector
$tooOld = 0;
// set the time selector
if ( ( time() - 1209600 ) >= $u_time ) { $tooOld = 1; } // 60*60*24*14 = 1209600 = 14 days
if ( ( time() - 15552000 ) >= $u_time ) { $tooOld = 2; } // 60*60*24*30*6 = 15552000 = 6 months
?>
Once I have the age of the article I look at inserting my adverts in whatever positions I think are best. For this I use the following code - again keeping it very simple.
<?php if( $tooOld > 0 ) { ?>
INSERT AD CODE HERE FOR POSTS 14 DAYS AND OLDER
<?php } ?>
<?php if( $tooOld > 1 ) { ?>
INSERT AD CODE HERE FOR POSTS 6 MONTHS AND OLDER
<?php } ?>
Something else I like to do is show different sized adverts in different categories so you can combine the two like so…
<?php if( $tooOld > 0 && in_category( 1 ) ) { ?>
INSERT AD CODE HERE FOR POSTS 14 DAYS AND OLDER AND ARE IN CATEGORY 1
<?php } ?>
Hiding Adverts
Something I have recently started using on Binary Joy is an additional little feature which lets me hide the adverts on certain posts. I find this helpful on site news posts, such as competition announcements, on which I don’t want ads to appear. To do this I add a custom field to the post saying to hide the adverts. You can call the it whatever you like but I have chosen display_ads as the field name. All you have to do to make use of it is add an extra ‘if’ statement after your age checking code.
// get the ad display properties
if( get_post_meta( $post->ID, "display_ads", true ) == "" ) { tooOld = -1; }
I set the $tooOld status to -1 so that I can use the value of 0 for other things - for example on Binary Joy I always show adverts (unless tooOld == -1), but I replace them with bigger and bigger ads as the value increases - so 0 is the smallest size.
In action
The best place to see me using this system is Binary Joy - my gaming site. Check out posts of different ages (in the archives) and you should be able to see the different positions available.
I think it’s a system that works really well and, as I said before, my adsense earnings are increasing - so it can’t be that bad an idea (I hope). I’d love to hear your opinions.

Comments »
August 30, 2007
This is an idea that I’d like to experiment with. However, I not much of a code jockey and can barely tell PHP from PSP. Where would you insert the above code in your template?
Thanks,
August 30, 2007
Ian - thanks for pointing that out. I should have mentioned it :S I’ve updated the article above a little to try and help. Basically I only add these ads on the single posts, either in the single.php template or in index.php. I place the code in the “wordpress loop”.
Does any of that make sense?
August 30, 2007
That was enough info for me to sort it out. I tried it on one of my less active blogs and it worked like a charm. Thanks for the help.
August 31, 2007
Interesting, and I won’t pretend to fully understand it
I understand what the loop is & where to insert code, but it reads like you do this on a post by post basis? Or have you just inserted this into single.php and once a post reaches a set age, the adsense drops in?
Sorry if I’m being thick, I’m one of those dangerous people who knows a little about PHP, but only enough to break things!
August 31, 2007
This is the kind of things that makes everyone happy: your dedicated visitors, not seeing your ads, and you, making money from older posts.
August 31, 2007
Chris - if you have a single.php template then stick it all in the loop in there. It will take care of everything else automatically - you certainly don’t need to do it per post.
Copes - that’s the plan
September 1, 2007
Nice. I had added your blog to my rss reader, more less just for that
. I just came across this via ProBlogger: http://www.dailyblogtips.com/s...
Which seems to do similar to yours and easier for the less techy people.
Cheers, Yourhighness
September 1, 2007
I made Who Sees Ads for this reason
September 1, 2007
this sounds like a good idea to me! Todd
September 3, 2007
Cheers Ben
December 24, 2007
I’m having a Blogger blog. Is there a possibility of implementing such a system in a blogpost blog?
People who link to this post...