Celebrity Style · Red Carpet · Get the Look
Tutorials

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.

Hit or miss?

What do you think of this look?

WordPress Display on Homepage Only: Show Only on Home Page
Up Next

WordPress Display on Homepage Only: Show Only on Home Page

Tutorials

Leave a Reply

Save
The Weekly Edit

Every look worth knowing, every Friday.

The best celebrity outfits of the week, the pieces behind them, and where to find them — in one beautifully edited email.

No spam. Unsubscribe anytime. See our Privacy Policy.