Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By pgrote on
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!
Comments
You can do this in the
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:
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.
(Username formerly my full name, Richard Eriksson.)
Thanks! I will give that
Thanks! I will give that shot!
Ok, I did that.
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
By this time, you might have figured it out already, but just in case:
In
node.tpl.php
, add this line: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)"
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 6 Word count
For Drupal 6.x, you just have to modify the code slightly:
In
node.tpl.php
, add this line:Add it, as above, on a new line just after line 4, which has
}?>
Then, make the following changes to the line that starts with
<?php if ($links)
(Basically, I just removed the
<div class="links">»
tag from the Drupal 5 version)As before, 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
Thanks for posting this, working well on my side.
Tim
Very handy but it might not be 100% accurate
Thanks for the code! What is a bit weird is that the count is not exactly the same as in both mircosoft word and open office writer. I made sure I only counted the words from the $content variable. Can't find a reason for this, It is pretty accurate though.
For example for one page the php code shows 184 words and word/writer show 189 words. (I had to copy paste the text in notepad first otherwise word/writer displayed completely wrong word counts) I am using this in the Ubiquity theme so maybe it has something to do with the theme. Does anyone else notices a discrepancy?
Drupal 6
Proper way to do this is more like...
In template.php:
And in node.tpl.php something like:
Better still, put the above in a tiny module. That's what I did. Will publish if I get chance. Only code in the module (called wordcount) is this:
--
https://www.codeenigma.com/
Another option is to save the
Another option is to save the word count when saving the node rather than recalculating it each time:
What about Drupal 7
Is there a way to print word count for the body field after node save in D7?
Think. Observe. Think. Conclude. Then talk.
www.globi.ro - just another travel guide