A few weeks ago, when I (neatly) blended my Google AdSense 336×280 block a couple of paragraphs down the post content I thought I did a smart job! As a matter of fact, this was really resulting in an increased CTR (Click-Through-Rate) as well and a few readers like Hussein were interested in knowing how I went about doing it? (comment)

However, a few other regular readers like Kim, Lax and Mitch had altogether different views on this topic. Out of their conversation in my last post it was obvious that the regulars who read your blogs on a daily basis (fresh posts that is) should not be presented with the ad block in between though it’s okay for the older posts (seen mostly by search & referral visitors). Now, the following code sample(s) explains how this was achieved.

How to insert an ad block for Older Posts alone?

Here, I am talking about inserting ad blocks in your single post view only and hence you can use this piece of code in a single.php file of your theme and nowhere else. Now, if you want to display your ads for only those posts older than say 7 days, I would use the following code:

Code to display ads only for older posts

You can change the value 7 to anything else like 10 or 30 so that the ad gets displayed on those posts older than 10 days and 30 days respectively.

How to insert the ad block well into the post?

Now if you want to display an ad block after, say, the third paragraph of the post, the following is what you could do:

Within the single.php, locate the code string the_content. It should look something like the image below:

single.php post rendering before commenting!

Now comment the whole line and include a new little module (yet to be written though we already call it single_post_split.php) below the commented code. The code portion within single.php will now look like:

single.php now includes single_post_split.php!

Add a file by name single_post_split.php to your theme folder and paste following code in that file and save it (Download the actual code from the following text file and rename it with .php extension)

single_post_split.php filled and saved!

This will now make sure that your ads get displayed after the third paragraph of all single posts that are older than seven days. If you don’t want the older post restriction, just remove the relevant condition in the ‘if’ statement.

You can add more such conditions to insert ad blocks at multiple locations within the post in conjunction with more styles to float them to left, right etc. Another sample code can be found here that displays a 250×250 ad block floated to the right after the first paragraph and a large rectangle ad block after the fifth paragraph.

It is also possible to implement conditions such as inserting ads for all posts except the last two or three etc but more such code could affect the performance of your blog rendering.

A Word of Caution

Back up your single.php theme file before attempting any modification to your theme code.

Comments to: How To Blend your AdSense Block within the WordPress Post Content & Display Conditionally?

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.