How to Insert Ads Between WordPress Content Paragraphs Without Plugin

By Focusoncode June 21, 2018

Advertisement plays a high role on online content. Everyone wants to get the most profit from it and they try different of ad positioning to check which of them performs better. Although there are a lot of ad positioning in a website, depending on the template, one position prefered by most of all webmasters is position inside articles.

By now most of the advertising networks support in article placement and it turns out that it is one of the most profit placement because if your content attract readers they will read the whole content and they will have to take a look at the advertisement.

Placing the ads dynamically inside article can be a little difficult. One of the tricks ca be to split the WordPress content by paragraph tag, create e loop and when we reach the third loop (Suppose we want ads to display after third paragraph) we show our ad. This add will be added to every post after the third paragraph.

Below we will post a code snippet that makes this possible. All you have to do is go to your WordPress template folder, open single.php or loop-single.php and replace the_content() with the following code:

$show_after_p = 5;
$content = apply_filters('the_content', $post->post_content);
if(substr_count($content, '<p>') > $show_after_p)
{
$contents = explode("</p>", $content);
$p_count = 1;
foreach($contents as $content)
{

if($p_count == $show_after_p)
{
?>
<!-- Ads goes here -->
<?php
}
echo $content;
echo "</p>";
$p_count++;
}
}

Now change the paragraph number in show_after_p  variable, where you want to display the advertisement and now we got our ad in every single post after n paragraphs.

 

Share This

Leave a Comment

Your email address will not be published.