Skip to content

How to Optimise : Time Based Adsense

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”.

// 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 ) { ?>
<?php } ?>

<?php if( $tooOld > 1 ) { ?>
<?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 ) ) { ?>
<?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.

Ben View All

Ben is a lifelong Nintendo fan who also likes to build websites, and develop games. He also buys way too much Lego.

13 thoughts on “How to Optimise : Time Based Adsense Leave a comment

  1. 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?


  2. 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?

  3. 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.

  4. This is the kind of things that makes everyone happy: your dedicated visitors, not seeing your ads, and you, making money from older posts.

  5. 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!

  6. 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 🙂

  7. Pingback: 98 Blog Tips for a Lazy Sunday
  8. Pingback: 10 Tips for Good Commenting Practice | Net Writing

Leave a Reply

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