
Since WpRecipes has been launched, I had numerous requests to create a function that can display the word count of a post content. So, here is a function to do it easily.

Since WpRecipes has been launched, I had numerous requests to create a function that can display the word count of a post content. So, here is a function to do it easily.
Simply open your functions.php file and paste this function in it:
function wcount(){
ob_start();
the_content();
$content = ob_get_clean();
return sizeof(explode(" ", $content));
}
Once done, you can call the function within the loop to get the number of words of the current post:
<?php echo wcount(); ?>
This recipe was inspired from this excellent tip.
2 Responses
Thank you for this function!
It can be used outside of WordPress as well though.
Might be just myself but this is only an approximate and not an exact value. There are small differences between the true post count, however it is close enough.
Thanks!
Trackbacks: