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= 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 Text / Ads Go 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.
Comments
4 Responses to “How to Insert Ads within your Post Content in WordPress”Share Your Opinions
Tell us what you're thinking...
and if you want a pic to show with your comment, then get gravatar!
Please make sure that you have read our Comment Policy.










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.
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.