DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

It is a well known fact that your regular visitors don’t click on your ads. In order to monetize your blog without annoying your loyal readers, what about displaying ads only when posts are older than 15 days?

To achieve this recipe, simply open your functions.php file and paste the following code in it:

function is_old_post($post_id=null){
   $days = 15;
   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;
}

Once done, you are now ready to call the functions in your single.php template as shown below:

<?php if(is_old_post()){ ?>
INSERT AD CODE HERE
<?php } ?>

That’s all, you’re done. By default, ads will be displayed on post older than 15 days. To change this value, simply edit the $days variable on line 2.

This recipe has been initially published on my other blog Cats Who Blog. If you haven’t yet, you shoul definitely visit it!

Tags

Leave a Comment

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