If you are using Wordpress publishing platform for your blog, you will find this code snippet useful. In some situations, you may want to add some content (advertisements, images,etc) to your blog post, only if your post length reaches certain number of words. In my case, I wanted to include Ads after my blog posts, only if the post has at least 100 words. I do not want to add advertisement for small posts. Below is the code snippet to achieve this.
Important:
This code snippet is dependent on Word count plug-in for Wordpress created by Murray. You need to include this plug-in in your Wordpress installation, before using the below code snippet. You can check the plug-in site for installation instructions.
Code snippet for including content based on post lenght:
For this code snippet, I assume you are including it in “single.php” file. Open the “single.php” file and add below code after <?php the_content('ยป Read the rest of the entry.. ') ?>
Code to include:
<?php
if (function_exists('mtw_wordcount'))
{
// Number of words to check for
$PostLengthToCheck = 100;
// Get the post length by calling the function mtw_wordcount.
// This function returns the number of words in a blog post.
$postLength = mtw_wordcount();
if ($postLength > $PostLengthToCheck)
{
//Add the content inide the quotes of echo statement.
// Content may be anything.
echo 'Your Content goes here';
}
}
?>
I hope the above code snippet will be useful to Wordpress bloggers. Let me know what you think.
Comments on this entry are closed.