为WordPress增加文章字数统计的办法

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中文博客文章字数统计代码

[reply]

增加办法与上述相反,首先把上面代码加到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); ?>

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

[/reply]

成果看这篇文章题目上面信息栏

以上就是安达网络工作室关于《为WordPress添加文章字数统计的方法》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress 文章字数统计
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress中获取页面链接和题目的相干PHP函数用法解析

get_permalink()(获取文章或页面链接) get_permalink() 用来依据固定衔接前往文章或许页面的链接。在获取...

WordPress中获取指定分类及其子分类下的文章数目

获取特定分类文章数 有时分咱们想获取某个分类(category)下的文章数目,以便在博客的某个中央显示进去。上面...

用wordpress制造网站的步骤

在没有本人的网站的时分很想领有一个属于本人的网站,能够说是本人的一个欲望吧。然而当我真正的运转起来了...

wordpress增加顶级菜单完成思绪

比方本人创立了一个主题,那么需求在后盾增加一些设置选项,所以就触及到了后盾增加顶级菜单的需要: 复制代...

封闭wordpress新用户注册邮件告诉的办法

本文实例讲述了封闭wordpress新用户注册邮件告诉的办法。分享给大家供大家参考。详细剖析如下: wordpress博...

wordpress更改用户列表排序(按注册工夫排序)教程

在 WordPress 后盾,用户是依照用户名排序的,并且没有显示注册工夫,假如咱们心愿可以在后盾看到用户的注册...

需求提交

客服服务

亿鸽在线客服系统