How to Insert Ads in the Middle of Your Content!

How to Insert Ads in the Middle of Your Content!

Anyways, here’s how to insert ads after 1-3 sentences after your content. (Or you can change the $tcount>=1 values to something like $tcount>=3 if you want to show ads after more sentences.)

You can even show additional ads at the bottom of your content if your blog post is a long blog post versus short.

Here’s the code that will let you insert ads at different places in your content.

You need to replace in your single.php file (usually) with the following code:

<?php
$content = apply_filters('the_content', $post->post_content); //get the post content store in $content
$save = explode("</p>", $content); //Separate the content into <p> blocks
$tcount=0; //this is count for number of <p> blocks
$adon=0; //this is a variable so you don't show ads more than once.
foreach($save as $item) {
echo $item; //print the <p> block
echo "</p>";
if(preg_match('/<p> /',$item)==0 && $tcount>=1 && $adon==0) {
$adon=1;
?>

Replace this with Your adsense code!!!

<?php
}
if(preg_match('/<p> /',$item)==0 && $tcount>=4 && $adon==1) {
$adon=2;
?>
Replace this with Your adsense code if you want additional adsense blocks for super long blog posts or leave it empty if you don't want one.
<?php
}
$tcount++;
}
?>

Source From here