Advertisement is the best and easy way to monetize your blog. Google Adsense, Chitika, Infolinks and many advertisement programs are available to earn some bulks. But too many ads on your blog may play a negative role. Readers do not like too many advertisements when reading a page as the content becomes bit distracted. So, why would you loss your loyalty for some extra penny?
The better thing you could follow and would be a good idea to hide your ads by Google or other ad providers from the newer posts, so that your regular readers can’t see them when reading the latest articles. In this article, I’ll describe the procedure to display ads only on those article which are older than a certain days or months. Before starting, I must say that there are some good WordPress plugins like Who Sees Ads, Adsense Manager, Adsense Deluxe etc. But I do not like to use plugin for those very purpose that can be easily achieved by adding some lines of code.
Display Adsense ads or other advertisements on the Older posts in WordPress
1. Adding the code to Functions.php
Add the following code into your theme’s functions.php file.
function is_older_post($post_id=null){
$older_than = 30; global $wp_query;
if(is_single() || is_page()) {
if(!$post_id) {
$post_id = $wp_query->post->ID;
}
$current_date = time();
$offset = $older_than *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;
}
[Thanks Jean-Baptiste Jung]
Note:
1. The code resides between ‘<? php’ and ‘?>’ .
2. In second line, set the value of $older_than as your preference. Here the value is 30 to show ads on the posts that are older than 30 days. 3. Save the function.php file.
2. Placing the Ad code
Now put the following code wherever you want to show your ads.
<?php if(is_older_post()){ ?> Put your Ad code here <?php } ?>
This tricks can be applied for both the banner ads like Adsense, Chitika and the text ads like Infolinks, Kontera etc. Just put the JavaScript code of the ad unit at the specified place. The function is_older_post() will check the post or page if that is older than your specified days. If so then it allows to execute the ad scripts, otherwise the scripts will be skipped. Currently, I’m using this method in this blog. What about you? Let me know.
Hi Tanmay,
Very Tutorial sure I will try in my non thesis blog. Moreover your blog looks more clean and refined.
Thanks for Sharing
-Imran
Thanks Imran. You can also apply this in your thesis theme. I’m glad that you liked the new look of my site. This will inspire me. Thanks.
Thanks for this great tip. I’ve just implemented it on my blog and it worked fine.
Glad that you find this useful. Keep reading our tutorials.
Apply for an urgent money support to your plan, in this finance we offer in low percentage Fast N’ Easy service world wide.offering funds in this company your confortibility is our proud do you have a bad record, have the bank reject you, and nobody willing to lend you money for your plan support contact us today via financeinvesmentservice@gmail.com for more details.