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.
- <?php
- if (is_single(’4′) || is_single(’20′) || is_single(’35′) || is_single(’58′))
- {
- ?>
- [ADSENSE CODE]
- <?php
- }
- ?>
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.
- <?php
- $today = date(’r');
- $articledate = get_the_time(’r');
- $difference = round((strtotime($today) - strtotime($articledate))/(24*60*60),0);
- if ($difference >= 30)
- {
- ?>
- [ADSENSE CODE]
- <?php
- }
- ?>
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!
Page Rank 5! I am somewhat jealous!
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!