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 (0)

Trackback URL

Leave a Reply




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

Please copy the string BUuXL8 to the field below: