Get words count of your wordpress post

January 26, 2009 · Print This Article

How to get and display the word count of a wordpress post?

The first thing to do is to create the function. Paste the following code on your functions.php file:

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 your single.php file and paste the following code:

Word count: <?php echo count_words($post->post_content); ?>

And word count will be displayed.


Your choice for site templates and wordpress themes

Comments

Got something to say?

You must be logged in to post a comment.