How to Show Adsense Ads Only On Longer Posts in WordPress

It is always a major concern to maximize revenue from posts that have a large word count. At the same time, you also don’t want to annoy the regular readers of your blog by showing them ads on shorter articles. Read along to know how to control number of ads for different articles.

Statistics and experience show that Adsense ads yields best when placed along the body of the articles. If you study the famous Adsense heatmap, you will notice that the following two positions get the maximum exposure:

  • The area under the post title.
  • The area where the content of the article ends.

Adsense heatmap and best placement positions

Showing Adsense ads both before and after the article can be annoying to the readers. Hence, a good idea would be to place a second Adsense unit on selected posts that have a word count greater than 700. You might like to place the second ad unit just at the end of the article because when the user has finished reading and he is likely to look for more resources. (see :Adsense split testing guide for WordPress and Blogger)

Here are the codes you need to use in the WordPress theme of your blog:

1. Open the functions.php file of your blog’s theme and paste the following code.

/** Count Post Word count **/
function wcount(){
$content = ob_get_clean();
return sizeof(explode(” “, $content));

2. Now open the single.php file of your blog’s theme and find the following code

<?php the_content(); ?>

3. Place the following code right after the above code

<?php if (wcount()> 500) {?>
Insert Adsense code here
<?php };?>

The above code shows Google Adsense ads on posts that have a word count greater than 500 (change the number at your convenience).


Please enter your comment!
Please enter your name here