How to Optimise : Time Based Adsense

In my Adsense tips post, I recently discussed the use of time-based ads and how they can be beneficial. Specifically, I like to display ads based on the age of the article, but please note that I only implement this technique on individual posts rather than the entire site.

There are several reasons why I find this idea appealing:

  • It prevents regular visitors from being overwhelmed with ads.
  • It allows me to avoid seeing the advertisements myself.
  • It provides search engine visitors (who typically end up on older posts) with relevant advertising, increasing the likelihood of ad clicks.

So, how do I accomplish this? Fortunately, it’s quite simple. I utilize the WordPress command get_the_time(‘U’) to retrieve the article’s creation time. Then, I calculate a specific number of days ago and compare it with the article’s age to determine its category. This code is placed within 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 determined the article’s age bracket, I strategically insert advertisements in suitable positions using the following code:

<?php if( $tooOld > 0 ) { ?>
INSERT AD CODE HERE FOR POSTS THAT ARE AT LEAST 14 DAYS OLD
<?php } ?>

<?php if( $tooOld > 1 ) { ?>
INSERT AD CODE HERE FOR POSTS THAT ARE AT LEAST 6 MONTHS OLD
<?php } ?>

In addition, I like to display different sized ads for different categories. To combine these two features, you can use the following code:

<?php if( $tooOld > 0 && in_category( 1 ) ) { ?>
INSERT AD CODE HERE FOR POSTS THAT ARE AT LEAST 14 DAYS OLD AND BELONG TO CATEGORY 1
<?php } ?>

Hiding Advertisements

I have recently implemented an additional feature on Binary Joy where I can hide ads on specific posts. This is particularly useful for site news posts or competition announcements where I prefer not to display any ads. To achieve this, I add a custom field to the post indicating whether to hide the advertisements. You can name this field as you wish; in my case, it is named display_ads. To utilize this feature, simply 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; }

By setting the $tooOld status to -1, I reserve the value of 0 for other purposes. For instance, on Binary Joy, ads are always shown (unless tooOld == -1); however, they are progressively replaced with larger ads as the value increases – with 0 representing the smallest size.

In Action

You can observe this system in action on Binary Joy, my gaming site. Explore posts of varying ages in the archives to witness the different available ad positions.

I believe this system works exceptionally well, and as I mentioned previously in my Adsense tips post, my Adsense earnings have witnessed growth. Therefore, I would appreciate hearing your thoughts and opinions.

Was it good/ useful/ a load of old rubbish? Let me know on Mastodon, or BlueSky (or Twitter X if you must).

Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

WordPress News

The latest WordPress updates from the WPBriefs Podcast.

Related Posts

27 May 2013

WordPress: 10 Years Young, What Does The Future Hold?

WordPress is now 10 years old. I started using wordpress 9 years ago – which means I joined the WordPress community early on. The reason I chose WordPress is simply because of the fabled 5 minute install process – I...
30 Mar 2010

10 WordPress query_posts tips you probably don’t know

I have written a really brief query_post tutorial before, and it did quite well, but both WordPress and my own skills, have advanced considerably since then. So I thought it would be interesting to revisit the query_posts command and see...