Black & White Notes

Wp Tips, How to Insert Ads within your Post Content in WordPress

Posted in WORDPRESS by dimazgyba on July 4, 2010

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: