Show Ads Based on Content Length
Filed in: Make Money Online, WordPress — February 17th, 2007Problem: 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]
< ?php
$countpost = $post->post_content;
$countpost = preg_replace(‘/\s+/’, ‘ ‘, strip_tags($countpost)); // remove white space and html
$words = ((count(explode(” “,$countpost)) + count(explode(“.\r”,$countpost)))-1); // count spaces and periods
if ($words > 100) {
?>
< ?php } ?>
[/php]
My two cents
This is a simple WordPress hack. I hope you find it useful to have more flexible control on ad placements.
Like this post? Please share:
Follow me on Twitter and Facebook. Subscribe to free newsletter for updates like this article..




Pingback: BlogTalks » Blog Archive » Wordpress Hack: Selectively Show Ads Based on Content Length
Pingback: Menampilkan Iklan Berdasarkan Panjang Isi Halaman | Cosa Aranda - NEW
Pingback: How to Show Ads based On Content Length? » Handry Business Journal
Pingback: Wordpress Hack: Anzeigenblock abhängig von der Postlänge anzeigen - vi-su
Pingback: How to Display Ads Depends on the Length of Content ? » WWW.ANGELLICA 2017.COM