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;
}else{
$return = false;
}
}else{
$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.

Related articles

Dell Inspiron i7559-3763BLK

Best Laptops Under $1500 for Everyday Usage

Bulk UnFollow Inactive Twitter Followers

How to See Follower Stats and Bulk UnFollow Inactive Twitter Followers

How to Control Google Play Music When Screen is Locked on Android

How To Control The Music App On Your Android When The Screen Is Locked

Browser Opening An Unknown Page At System Startup

Is Your Browser Opening An Unknown Page At System Startup? Here is The Fix

1 comment

  1. Sodikin M says:

    the following code, an error is declared. How to fix it?
    $test = $current_date – $cunning_math;

Leave a Reply

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