Show Ads Based on Content Length

Filed in: Make Money Online, WordPress — February 17th, 2007

advertisement

Problem: Currently I am displaying two blocks of ads on each blog post, one at the top of post, the other at the end of post.

The ad placement is ok but it becomes a problem when I have short posts, which looks like a full page of ads with hidden content. I want to selectively show second ad block only if content is more than 100 words.

By referring to a word count code on WordPress Support, I managed to hide second ad block if content is less than 100 words.

Here is the code. It is for WordPress only. Add the following code after "the_content" function in the "single.php" of your WordPress theme.

PHP:
  1. <?php
  2. $countpost = $post->post_content;
  3. $countpost = preg_replace('/\s+/', ' ', strip_tags($countpost)); // remove white space and html
  4. $words = ((count(explode(" ",$countpost)) + count(explode(".\r",$countpost)))-1); // count spaces and periods
  5. if ($words> 100) {
  6. ?>
  7. <!-- YOUR AD CODE HERE-->
  8. <?php } ?>

My two cents

This is a simple WordPress hack. I hope you find it useful to have more flexible control on ad placements.

Popularity: 3% [?]

Bookmark and Share

Read also:

What do you think? 7 Responses to “Show Ads Based on Content Length”

Comments Feed | TrackBack URL
  1. #1
    mirul Says:

    emm.. thanks for the tips ;-)

  2. #2
    BlogTalks » Blog Archive » Wordpress Hack: Selectively Show Ads Based on Content Length Says:

    [...] Usually, we place our ad blocks below or above the content. While this placement is OK for posts with good lengthy content, it would look ugly on posts with short content. LiewCF has come up with a solution to selectively show the second ad block only if the post’s length is above a certain threshold of word counts. [...]

  3. #3
    Perniagaan Internet Says:

    Very informative and practical blogging tricks.

    Happy Chinese New Year LcF!

  4. #4
    Menampilkan Iklan Berdasarkan Panjang Isi Halaman | Cosa Aranda - NEW Says:

    [...] Liew CF, salah satu “pro” blogger terkenal dari Malaysia, beberapa waktu lalu memposting tips untuk menampilkan iklan secara selektif, yaitu berdasarkan panjang dari content suatu halaman. Trik yg hanya bisa digunakan untuk platform Wordpress ini (sorry Blogspot ) menentukan layak tidaknya suatu iklan dimunculkan berdasarkan jumlah kata yg ada di halaman tersebut. Salah satu keuntungan dari penggunaan trik ini adalah kita tidak perlu menjejali halaman dengan iklan apabila kebetulan kita melakukan posting singkat. [...]

  5. #5
    How to Show Ads based On Content Length? » Handry Business Journal Says:

    [...] Referring from LiewCF Blog Show Ads Based on Content Length [...]

  6. #6
    Wordpress Hack: Anzeigenblock abhängig von der Postlänge anzeigen - vi-su Says:

    [...] Post üblicher Länge noch passt, sieht es bei kurzen Posts einfach schrecklich aus. LiewCF hat hierfür einen Hack veröffentlicht, der es ermöglicht, den zweiten Anzeigeblock [...]

  7. #7
    How to Display Ads Depends on the Length of Content ? » WWW.ANGELLICA 2017.COM Says:

    [...] few months ago, Liew CF – One of Malaysian pro-blogger write a post about trick for displaying advertisements depends on the length of content. Unfortunately, that trick only applied for Wordpress platform. That trick automatically [...]

Comments are closed. Submit your comment here