WordPress Tip: How to Show Advertisements Only on Older Posts

Online advertising programs like Google Adsense, Infolinks, Chitika are some of the easiest ways to monetize a blog or website. There is no need to hunt for advertisers, you can sign up for a free account and paste a simple JavaScript code to get started.

But using too many advertisements on your blog can be a bit distracting since visitors may have a tough time reading the content you have written. This applies to banner ads as well as in text advertising programs like Kontera, Infolinks –  they pick random keywords from the body of your article and turn them into pop up ads when a visitor hovers his cursor on the keyword.

It may be a good idea to hide these advertisements on newer articles so that the regular readers, who visit your blog every other day do not see any advertisements.

WordPress Plugins ?

There are a good number of WordPress plugins available to customize how advertisements appear on your website or blog. Some of the very popular ones are Adsense Manager, Adsense Deluxe and Who sees ads. You can also search for a suitable plugin on the WordPress plugin directory and use it on your blog.

On this blog, I rarely use a WordPress plugin when the same thing can be achieved with a manual function or a raw php code. If you want to hide advertisements or any other piece of code on newer posts, here is a simple function which can help:

Show Adsense Or Other Advertisements On older posts in WordPress

1. Open the functions.php file for editing and paste the following function.

function is_old_post($post_id=null){
$days = XX;
global $wp_query;
if(is_single() || is_page()) {
if(!$post_id) {
$post_id = $wp_query->post->ID;
$current_date = time();
$offset = $days *60*60*24;
$post_id = get_post($post_id);
$post_date = mysql2date(‘U’,$post_id->post_date);
$cunning_math = $post_date + $offset;
$test = $current_date – $cunning_math;
if($test > 0){
$return = true;
$return = false;
$return = false;
return $return;

If your site’s theme does not have a functions.php file, create one.

2. Set the value of days in line number 2 e.g $days=25; .

3. Now paste the following code in your single.php file or in any other part of the WordPress theme where you want the advertisements to appear.

<?php if(is_old_post()){ ?>
enter code for advertisements
<?php } ?>

4. Done.

The above code calls the earlier function which checks the age of a post or page. If the age is less than your specified value, the advertisements are not shown. If the age is more than your preferred value, the ads are shown. You can use the same code to hide adsense ads or hide Infolinks ads on newer posts, just paste the JavaScript code of the ad units and it’s done. A wonderful recipe by Jean-Baptiste Jung

Example: On this blog, I use Infolinks on older articles and do not show them to my regular readers. This is because of two reasons – first, regular readers do not convert well with Infolinks or any other Ad network. Second, the in text advertisements sometimes becomes very obtrusive – why lose a reader for just 25 cents ?

Some more tips on using advertisements in a WordPress blog and customizing how they are shown:

Do you reward your regular visitors with Ad free content ? What methods or plugins do you use ? Share your ideas in the comments.

Don't miss the Free Book

101 websites for getting things done - an informative book which will make you more productive and efficient. Subscribe to get it for free.