Navnish Bhardwaj
Navnish Bhardwaj

Reputation: 1718

Word count not showing on posts?

I thought it might be an issue with a plugin I'm running, but I'm not seeing a word count on any of my posts on WP 4.0. I've deactivated all plugins, and that hasn't fixed me.

Well, not seeing is probably not very accurate - word counts are all 0, even on posts I've written pre-4.0. I'm running roughly latest PHP5.5 and MySQL 5.5 on the server, if that's at all relevant. The database looks fine on the surface, so am I missing something obfious?

Upvotes: 0

Views: 196

Answers (1)

Mike
Mike

Reputation: 8877

You've noted that this is to be used on the frontend of the site, so you can achieve it without the use of a plugin.

Place the following code in your functions.php file:

function word_count() {
    $content = get_post_field( 'post_content', $post->ID );
    $word_count = str_word_count( strip_tags( $content ) );
    return $word_count;
}

You can then use the function word_count() in your theme. word_count() returns the word count of the post, so you'll need to echo it out:

<?php echo word_count(); ?>

Upvotes: 1

Related Questions