How to insert ads after paragraph of wordpress post

Recently, my friend told me I should place my adsence code after the 1st paragraph of my post, according to his self-experience, this way will improve the CTR, so I tried many ways to insert my adsense code after the 1st paragraph, there are some plugins can do so, but I don’t like plugins, finally, I found the following 2 ways are better:

Both way is to edit the single.php of the template you are using, the first way is:

2. The second way is also to find this code in the single.php

<?php the_content(); ?>

Replace with following code:

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

		if($p_count == $show_after_p)
		{
		?>
                INSERT ADSENSE CODE or Any other ads code
		<?
		}
		echo "</p>";
		$p_count++;
	}
}
?>

After replace, it will automatically display your Adsense or any other ads unit after the first paragraph. You can modify $show_after_p = 1, which will control which paragraph you want the unit to display after.

The bug of this code is: if the $show_after_p value exceeds the amount of paragraphs there are in a post, the ad unit won’t display.

If anyone knows a better solution, please let me know. And if anyone decides to use the code, let me know if it works for you.

Filed Under: Site BuildingWordpress Themes

Tags:

About the Author: An expert in making money online, a freelance English-Chinese translator with 8 years experiences.

RSSComments (2)

Leave a Reply | Trackback URL

  1. Peter P says:

    It works great for me!

    THANK YOU SO MUCH!!!

    I’m not using it to insert ads but rather to insert a call to action.

  2. Vibram Five Finger shoes simulates walking barefoot, which moves your body in a more natural and healthy way.Vibram FiveFingers stimulate muscles in your feet and lower legs to build strength and improve range of motion. They’re the funnest shoes you’ll ever wear! You can’t beat shopping at vibramsaleshoes.net with free shipping.
    Pertaining to soccer people, the best choice for yourself is definitely TN NIKE PAS CHERbasketball shoes and boots. To get instructors, Nike training companies are fantastic for an individual. To get joggers, Nike jogging shoes can certainly raise your swiftness along with ruduce the actual sensation connected with weary

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

Please copy the string hM90kO to the field below:


three × 9 =