I just wanted to post this quick tips article as I am sure it will be helpful to other webmasters. If you are looking for a way to put adsense ads after the first paragraph of each entry in your wordpress blog OR if you want to place an adsense ad block after second or third paragraph of each entry, then here is what you can do.
The following need some simple code editing in wordpress template file (single.php)
1. Find the php file – single.php in your wordpress blog themes folder.
2. search for the code: <?php the_content(); ?>
3. Replace the above code with the following:
<?php
$paragraphAfter= 1; //display after the first paragraph
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++) {
if ($i == $paragraphAfter) { ?>
<div>
Your add code here
</div>
<?php
}
echo $content[$i] . "</p>";
} ?>
You can now place your adsense ad code into the place where it is written “Your ad code here”. You can also change the number of paragraph after which you would like your adsense ad to appear. This can be done by replacing the number in the code $paragraphAfter= 1 with the paragraph number after which you want the ad to be seen. I sure hope this is helpful.
Can you tell me how to do this in Drupal? thanks.
Thanks a lot, this is exactly what I was looking for. I modified it a bit to also automatically add an adsense ‘link unit’ after the third paragraph. I’m no developer, but it was pretty easy to figure out by following along with your code. I think the last thing I’m gonna do is figure out how to automatically add it right before the last paragraph.
Its a lot of ads, but for some of my sites that are primarily to get ad clicks, its fine. I just want to make sure I use all 3 of my link units.
Thanks again!
Thank you for the tip ! I would like to don’t show ad when i have less than 3 paragraph on my post. Could you help me please ? Thanks
Hi.
Thanks, it works.