Inserting an ad between paragraphs in a WordPress post

Last updated on December 15, 2009. Tags: ,

You might have noticed a small change in my layout these past few days. The "summary" part on top is gone, and the Adsense ads below it is moved under the first paragraph.

Placing ads between paragraphs of the main content is one of the most effective strategy in monetizing your website. It's a similar concept as putting the ads between parts of a television show. It's certain that visitors will notice them as they scroll down reading your content .

One way of doing this is to manually insert the ad code between post paragraphs. This method is inconvenient because you need to insert the ad code every time you write a new post.

The other way is what we will discuss in this article. To insert an ad unit between the first and the second paragraph of a post, open the single.php file and look for the following line of code:

<?php the_content(); ?>

This code may not appear exactly as shown above. Sometimes there are stuff between the parentheses like for example <?php the_content('Read more...'); ?>.  Replace this code with the one shown below:

<?php
$paragraphAfter= 1; //shows the ad after paragraph 1
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
for ($i = 0; $i <count($content); $i++) {
if ($i == $paragraphAfter) { ?>
<!-- START OF AD CODE -->
PASTE AD CODE HERE
<!-- END OF AD CODE -->
<?php
}
echo $content[$i] . "</p>";
} ?>

In the above code, you can insert the ad code provided by Adsense (also works in Chitika, Bidvertiser, etc) in the line labeled as PASTE AD CODE HERE. The number 1 in the second line indicates that the ad will appear after the first paragraph. In case you want the ad to appear after the second or third paragraph, just replace 1 with 2 or 3 respectively.

This tweak also works with WordPress static page, just look for the <?php the_content(); ?> code in the page.php instead of single.php.

Posted by Greten on October 7, 2009 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Piseth on 05.19.10 2:57 am

    This is amazing, it works very well on my blog. You can check it out. thanks

  2. Posted by Smart Blogging on 06.06.10 9:17 am

    thanks for share tutorial insert ads between post content :)

  3. Posted by Tony Tran – Android How to on 08.10.10 9:04 am

    I just searching it for my wordpress blog and get success. Thank you for your great tips.

    Edit by Greten: Please include your actual name in the name field whenever you comment. Found your name in the about section of your website. Thanks!

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.