Warning: mysqli_query(): (HY000/1030): Got error 28 from storage engine in /home/wpthemes/public_html/wp-includes/wp-db.php on line 1877
How to Get the Word Count in a Post | WPThemes.com - WordPress Themes
Warning: mysqli_query(): (HY000/1030): Got error 28 from storage engine in /home/wpthemes/public_html/wp-includes/wp-db.php on line 1877

April 7, 2009

How to Get the Word Count in a Post

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]))
$words++;
}     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!


Previous Post and Next Post


Try HostGator Webhosting for 1 cent for the 1st Month Click here and enter "wordpress"

Comments

  • TechZoomIn

    July 3, 2009 at 1:57 am

    Good one just searching to the count..Thanks for the info….

Leave a reply

* means field is required.

*

*