Placing Google AdSense in Older Content with WordPress

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!

Next Post
Mozilla Labs: Prism
Previous Post
Education Attainment

Send a Message Have something to say? Use the form below to email your comment directly to me. If warranted, I’ll do my best to respond in a timely matter.

What is my first name?
(Hint: It’s David)

Portfolio
Screen shot of the About page for www.sbts.edu

Content © David Yeiser, 2009 | Privacy Policy | Contact