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(){
ob_start();
the_content();
$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).

Email this article

Written by on Thursday, February 18th, 2010

  1. Reader Comments

  2. Webmaster

    Great tip!

    What about the CTR for the lower ad?

    February 19th, 2010
  3. Amit Banerjee

    @Webmaster: CTR depends upon the blog… this tutoeial describes a way to show ads on posts that have a word count larger than a number.

    February 19th, 2010
  4. Udegbunam Chukwudi

    Will this hack work with cache plug-ins?

    February 28th, 2010
    • Amit Banerjee

      @udegbunam: Yes, because the functions.php file is executed before any other plugin. Cache plugins just store the local static files. The function checks the post word count and renders the ad code if your article has more word count then the one specified in function settings

      February 28th, 2010
  5. Udegbunam Chukwudi

    Thanks! Finally do you know of another trick for showing ads to search engine visitors only while using cache plug-ins?
    Thanks

    February 28th, 2010
    • Amit Banerjee

      @Udegbunam: Yes..there is a functioon to show Advertisements only to search engine visitors. I will come up with a post in a day.

      February 28th, 2010
  6. Udegbunam Chukwudi

    @Amit: YES! Can’t wait. All the plug-ins I’ve tried just don’t work with my cache plug-in. Hopefully your post will solve my problems once and for all.
    Cheers and have a great Sunday ;)

    February 28th, 2010
  7. Jack

    I do not like to copy and paste adsense codes. So, I use plugins at my blog. The plugin I use is Best Google Adsense
    http://wordpress.org/extend/plugins/best-google-adsense/

    April 23rd, 2011

Add Your Comment

All Comments are Moderated.

YOUR COMMENT

About This Site

Ampercent is a technology blog on computer tutorials, software guides, how to tricks and web tools. The blog is updated daily and written by two computer science students from India. Read More »

Tip Us »

Have a great tip which you want to share with fellow readers? Send in your ideas to tips@ampercent.com