Display word count for stories

pgrote - October 18, 2005 - 00:00

Hello,

Is there an exiting option to display the number of words in a story? I looked at the options and couldn't find it.

I'd settle for it showing under RECENT POSTS, in the Read Comments line after story, etc. Just somewhere it is displayed.

Thanks!

You can do this in the

Richard Eriksson - October 18, 2005 - 00:56

You can do this in the theme.

In a PHPTemplate theme, put the following code in node.tpl.php where you want the word count to display:

<?php
$wordcount
= count(explode(" ", strip_tags(trim($node->body))));
print
$wordcount;
?>

If you just want this to display in stories, copy the exising node.tpl.php file to node-story.tpl.php, and insert the above code.

Thanks! I will give that

pgrote - October 19, 2005 - 22:39

Thanks! I will give that shot!

Ok, I did that.

pgrote - October 22, 2005 - 07:23

Ok, I did that.

It didn't turn out right. lol

Couple of issues:

1) On the front page the word count shows as 1 no matter what.

2) I can't figure out where to put it so that it goes after this:

» add new comment | 7 reads | unsubscribe post

Any ideas?

Thanks again!

Here's one way

Robert Daeley - June 2, 2006 - 21:34

By this time, you might have figured it out already, but just in case:

In node.tpl.php, add this line:

<?php $wordcount = " | ".count(explode(" ", strip_tags(trim($content))))." words" ; ?>

I added it on a new line just after line 4, which has "}?>" in my version of the file. Then, make the following changes to the line that starts with "<?php if ($links)"

<?php if ($links) { ?> <div class="links">&raquo; <?php print $links; print $wordcount ?><?php }; ?>

And that should take care of both issues. One caveat -- if you have any modules activated that add stuff to the $content block (e.g. the nodevote module), it will throw off the count.

 
 

Drupal is a registered trademark of Dries Buytaert.