How to Get the Word Count in a Post

By | April 7, 2009

Thanks to WPRecipes, I have found an amazingly helpful tutorial how to get the word count in a post to show up on your index, or on your single page if you prefer. Here’s how we go about doing this.

Okay, first, open up your functions.php file in your themes directory, and paste the following code anywhere within:

function count_words($str){
   $words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
}     return $words;

Then, open up whereever you want the word count for your post to show up, be it the index.php or single.php, and paste this code anywhere within it:

Word count: post_content); ?>

And voila! The word count for your posts will be displayed!

Category: Uncategorized