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.
This was an incredible post. You've saved us all from lackluster CTR! A++
thanks.. i'll try.
hi, this code is ignoring [/caption] therefore jumping inside caption which apparently qualifies as first paragraph. Any ideas?
But what when I need paste 2 ads? One after one paragraph and second after third paragraph.
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
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.
Thanks for this post, it saved a lot of time on the adding adsense code on each and every post on the site.
- spam
- offensive
- disagree
- off topic
Like