为WordPress添加文章字数统计

WordPress21715.6K阅读模式
摘要  Wordpress在后台编辑日志时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢?研究了一下程序源文件,发现中文版WP后台的字数统计功能,是...

WordPress在后台编辑日志时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢?研究了一下程序源文件,发现中文版WP后台的字数统计功能,是通过wp-content\languages目录的zh_CN-word-count.js实现的,就是不知道如何调用。网上搜了一下,找到两篇老外给出的代码:

代码一、把下面代码加到主题的functions.php文件中:

  1. function count_words($str){
  2.     $words = 0;
  3.     $str = eregi_replace(" +"" "$str);
  4.     $array = explode(" "$str);
  5.     for($i=0;$i < count($array);$i++)
  6.      {
  7.         if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]"$array[$i]))
  8.             $words++;
  9.     }
  10.     return $words;
  11. }

然后在single.php中希望显示字数统计的位置加上:

  1. Word count: <?php echo count_words($post->post_content); ?>

原文

代码二、还是将下面代码加到functions.php文件中,此方法与上面不同的是,还加上了一个估算的阅读时间:

  1. // Custom functions
  2. // START : Show word count
  3. function show_post_word_count(){
  4. ob_start();
  5. the_content();
  6. $content = ob_get_clean();
  7. return sizeof(explode(" "$content));
  8. }
  9. // END : Show word count
  10. // START : Estimated reading time
  11. if (!function_exists('est_read_time')):
  12. function est_read_time( $return = false) {
  13. $wordcount = round(str_word_count(get_the_content()), -2);
  14. $minutes_fast = ceil($wordcount / 250);
  15. $minutes_slow = ceil($wordcount / 150);
  16. if ($wordcount <= 150) {
  17. $output = __("< 1 minute");
  18. else {
  19. $output = sprintf(__("%s - %s minutes"), $minutes_fast$minutes_slow);
  20. }
  21. echo $output;
  22. }
  23. endif;
  24. if (!function_exists('est_the_content')):
  25. function est_the_content( $orig ) {
  26. // Prepend the reading time to the post content
  27. return est_read_time(true) . "\n\n" . $orig;
  28. }
  29. endif;
  30. // END : Estimated reading time

同样在single.php中希望显示字数统计的位置加上:

  1. The following <?php echo show_post_word_count(); ?> words should take about <?php echo est_read_time(); ?> to read.

原文

可惜上述两种方法对汉字统计无效,只适合纯英文站点,网上也没发现与中文博客字数统计相关的文章,没办法还是自己折腾一个吧。

WordPress中文博客文章字数统计代码

添加方法与上述相同,首先把下面代码加到functions.php文件中。( 注:HotNews主题加到“//全部结束”前面 )

  1. //字数统计
  2. function count_words ($text) {
  3. global $post;
  4. if ( '' == $text ) {
  5.    $text = $post->post_content;
  6.    if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '本文共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') . '个字';
  7.    return $output;
  8. }
  9. }

再把调用统计代码加到自己认为适合的位置。

  1. <?php echo count_words ($text); ?>

经测试对中文统计没有什么问题,英文统计的是字母。

效果看这篇文章标题下面信息栏

这个评论可见功能参见:WordPress博客评论可见实现方法

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  217  访客  196  作者  5
    • 东子
      东子 1

      不错,学习了。

      • 小淅
        小淅 1

        如何实现类似于本博客的底部的提示信息呢?

        • 清心涟漪
          清心涟漪 1

          学习了,并在博客里应用上。

          • 一段佳能
            一段佳能 0

            有没有办法实现统计整个网站的文章字数总计、

            • 腾蛙网
              腾蛙网 4

              不错的功能

            匿名

            发表评论

            匿名网友
            :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

            拖动滑块以完成验证