Display Ads After the 1st Paragraph in WordPress

Usually we insert the advertisement just like Google Adsense or some CPC ads before the content or at the end of the post, and it is easy to float your ads on the left, right or in center, but most of us don’t know how to insert the advertisement after the first paragraph or second paragraph, here is a way to solve this problem.

1. First of all, open your single.php (for pages, open the page.php) file and look for the content code that would look something like this:

<?php the_content(); ?>

2. Then you can replace that code with the codes below:

<?php
$paragraphAfter= 2; //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.

Leave a Reply

You must be logged in to post a comment.