How to Insert Ads within your Post Content in WordPress

Posted on February 23rd, 2010 by in Tutorials | 16 Comments  
How to Insert Ads within your Post Content in WordPress

Have you seen blogs that has ads inside their post content? These ads are either after the first paragraph or the second paragraph in most cases. Most beginners wonder whether these sites insert ads manually when they write the content, or if there is a special code for this. It would seem extremely inefficient if one enters the code manually specially if you had to switch advertisers after one year and you have over 200 articles. This article will show you a snippet that will let you enter ads within the post content after the first paragraph.

First open your single.php file and look for the content code that would look something like this:

<?php the_content(); ?>

Replace that code with the codes below:

<?php
$paragraphAfter= 3; //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>Insert Ads Here</div>
<?php }
echo $content[$i] . "</p>";
} ?>

You may change the number of paragraphs by changing $paragraphAfter line.

Source of this article is suspended by the host, we used Google Cache to get this tutorial and it works.

About

Editorial Staff at WPBeginner mainly Syed and David.

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Thanks for this post, it saved a lot of time on the adding adsense code on each and every post on the site.

Rod60000 5 pts

This was an incredible post. You've saved us all from lackluster CTR! A++

Lisa Mason Lee 5 pts

hi, this code is ignoring [/caption] therefore jumping inside caption which apparently qualifies as first paragraph. Any ideas?

leesniak 5 pts

But what when I need paste 2 ads? One after one paragraph and second after third paragraph.

wpbeginner 50 pts moderator

leesniak You can add an elseif statement.

How could this be modified to be used on the front page with posts?

Hi, thank you for this ... unfortunately it will not work for me ... my post simply loops forever and ever (or something) ... it just hangs and does not load. The page does not load, the browser 'loading' icon just turns and turns. I am using WP 3.0.4

Had to fix line 5 ... i++

for ($i = 0; $i <count($content); $i++) {

Thanks!

@Joe

Thanks for the loop fix, I was trying to figure it out myself.

Hi,

Could someone please tell me how to do this for a page?

The exact same way. Just open your page.php file and paste the codes.

Great idea, especially when you want the user to notice the ads more- that could also annoy the user. maybe there should be an option to hide them if you dont want it distracting the viewer?

you missed the underscore in the first block of code.

Thank you for the correction. Just fixed it.

You can show ads only to google referrers only. Or use a jQuery script that has cookies installed. So ads will show for every user, but if they exit it, then they won't see it. Until ofcourse they clear their cookies.

You can try Ozh' Who Sees Ads Plugin to determine who you want to see ads on your blog. You can exclude ads regular users, or you can show ads only for those people who came to your blog from Google

Tweets about us: