Post Pic

WordPress hack : How to display ads on old posts only

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!

11 Responses

Dec 07 2009 08:53

Thanks for the tip! It can be helpful to place ads on older posts, which your regular readers won’t be reading anyway. That way, they won’t be bombarded with ads.

Dec 07 2009 09:03

Nice Idea :)

Dec 07 2009 09:17

simple but really useful for newbies

Dec 07 2009 11:05

This is a good one! I wrote my own code to do this, but it was not nearly as neat as this.

Thanks mate!

Dec 08 2009 04:18

Good idea!

This is how smart looks as though we are not so hasty to ads

Dec 08 2009 15:10

Salut JB ! Sympa le bout de code. Si tu veux voici une version un peu plus concise : http://tinypaste.com/32096 ;)

Dec 08 2009 18:02

Excellent post! Really a good idea and great way to monetize all of those old blog posts that really only get hits with the search engines. Thanks!

Dec 11 2009 14:30

That’s something I have been using on my blog that receives many visits from daily users. They just don’t click to ads, so I coded the HTML to show the ads after ten days after the article’s been published.

This way you avoid low paying clicks plus your readers don’t get annyoed with too many ads.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required