Code #14: Display Post Word Count

Hello guys, and wel­come to the four­teenth day of “31 Days of Cod­ing”. Today we’re going to talk about dis­play­ing the post word count on the sin­gle posts.

First, you need to put the fol­low­ing code in the functions.php file of your theme:

func­tion wcount(){
ob_start();
the_content();
$con­tent = ob_get_clean();
return sizeof(explode(“ “, $con­tent));
}

Then, go to your single.php file of your theme, and in the loop, enter the fol­low­ing code where you want the count to display:

<?php echo wcount(); ?>

And that’s all. That will dis­play the post word count on your theme.

blog comments powered by Disqus