Prev

Next

 
Section Four

Notebook

A quick tutorial on how to place Google AdSense in posts that are older than X amount of days. (Where X is the number of days you choose, 30, 7, 100, etc.)

 

Recently I decided to put Google AdSense on my site, mainly out of curiousity to see if you could actually make any money. However, I didn’t want to litter my site with it, so I decided to place it on several tutorial posts and the Trevilian Way download page (WordPress theme).

To do this, I added a few lines of PHP code in the single.php template to display the AdSense javascript if the post ID’s matched the ID’s of the tutorial posts. The code for this is shown in Figure 1.

  1. <?php
  2. if (is_single(’4′) || is_single(’20′) || is_single(’35′) || is_single(’58′))
  3. {
  4. ?>
  5. [ADSENSE CODE]
  6. <?php
  7. }
  8. ?>
Figure 1: Original code used to display the AdSense, where 4, 20, 35 & 58 were the posts on which I wanted to display the AdSense.

I decided a few days later that I wanted something a bit more automated, such as a time-based qualifier. I figured there was some way that you could determine the age of a post relative to the current time; and then use that data with logic statements to determine whether or not to display the AdSense.

A quick search on the WordPress Support Forums gave me exactly what I needed. (Thanks, blepoxp!) So then I went back to the single.php template and inserted the new code in place of the old. You can see this code in Figure 2.

  1. <?php
  2. $today = date(’r');
  3. $articledate = get_the_time(’r');
  4. $difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);
  5.  
  6. if ($difference >= 30)
  7. {
  8. ?>
  9. [ADSENSE CODE]
  10. <?php
  11. }
  12. ?>
Figure 2: New code used to display the Adsense.

That’s it! Now AdSense is displayed on every post older than or equal to thirty days. Lastly, for those wondering, after about a week I’ve made $0.24. So I’m not quitting the day job yet!

Commentary

1. Oct 29, 2007 Aaron Marshall says

Page Rank 5! I am somewhat jealous!

2. Oct 29, 2007 David Yeiser says

Ha! I don’t have cool videos on my blog like you though. If Google could transcribe video then I’m sure you would be upwards of 11 or 12!

Join the Discussion




*Required


 
 
04.Notebook