QQ咨询不加好友发不了信息,咨询前先加好友! → QQ:820896380

WordPress 前台编辑器加上字数统计功能

WordPress在后台编辑文章时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢 wordpress WordPress在后台编辑文章时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢?研究了一下程序源文件,发现中文版WP后台的字数统计功能,是通过w...

WordPress在后台编辑文章时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢

WordPress 前台编辑器加上字数统计功能
wordpress

WordPress在后台编辑文章时编辑框左下角有一个字数统计,不过只显示在后台,能不能在前台也加上文章字数统计功能呢?研究了一下程序源文件,发现中文版WP后台的字数统计功能,是通过wp-contentlanguages目录的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) . "nn" . $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); ?>

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

请我们喝杯咖啡,谢谢^_^

给TA打赏
共0人
如本文“对您有用”,欢迎随意打赏,金额不重要,认可最重要!
    豆包可以帮你高效完成AI问答、AI对话、提供软件相关教程以及解决生活中遇到的各种疑难杂症,还能帮助你进行AI写作、AI绘画等等,提高你的工作学习效率。
    !
    你也想出现在这里?立即 联系我们吧!
    信息
    个人中心
    购物车
    优惠劵
    今日签到
    搜索