How to put Adsense ads block after first paragraph in WordPress Posts

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.

This entry was posted in Blogging Tips, Making Money. Bookmark the permalink.

6 Responses to How to put Adsense ads block after first paragraph in WordPress Posts

  1. Nikhil says:

    Can you tell me how to do this in Drupal? thanks.

  2. Brian says:

    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!

  3. Kwaelbi says:

    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

  4. Erfo says:

    Hi.
    Thanks, it works.

  5. admin says:

    test

  6. j says:

    Thanks for the code. However, it seems to add an un-needed closing p tag. Anyway to remove the extra p tag?

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>