WordPress hack : How to display ads on old posts only

by Jean-Baptiste Jung on December 6, 2009

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!

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

WordPress hack : How to display ads on old posts only