Post Pic

WordPress function to display your posts words count

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

Jul 31 2012 23:05

Thank you for this function!
It can be used outside of WordPress as well though.

Oct 28 2012 01:40

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:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox