激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

DEDECMS|帝國CMS|Discuz|PHPCMS|Wordpress|ZBLOG|ECSHOP|蘋果CMS|極致CMS|CMS系統|

服務器之家 - 建站程序 - Wordpress - 為WordPress添加文章字數統計的方法

為WordPress添加文章字數統計的方法

2019-09-27 11:46WordPress技術網 Wordpress

WordPress在后臺編輯日志時編輯框左下角有一個字數統計,不過只顯示在后臺,能不能在前臺也加上文章字數統計功能呢?研究了一下程序源文件,發現中文版WP后臺的字數統計功能,是通過wp-content\languages目錄的zh_CN-word-count.js實現的

WordPress在后臺編輯日志時編輯框左下角有一個字數統計,不過只顯示在后臺,能不能在前臺也加上文章字數統計功能呢?研究了一下程序源文件,發現中文版WP后臺的字數統計功能,是通過wp-content\languages目錄的zh_CN-word-count.js實現的,就是不知道如何調用。網上搜了一下,找到兩篇老外給出的代碼:

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

  1. functioncount_words($str){ 
  2.  
  3. $words= 0; 
  4.  
  5. $str=eregi_replace(" +"," ",$str); 
  6.  
  7. $array=explode(" ",$str); 
  8.  
  9. for($i=0;$i 
  10.  
  11.  
  12. if(eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]",$array[$i])) 
  13.  
  14. $words++; 
  15.  
  16.  
  17. return$words; 
  18.  

然后在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. // END : Show word count 
  9. // START : Estimated reading time 
  10. if (!function_exists('est_read_time')): 
  11. function est_read_time( $return = false) { 
  12. $wordcount = round(str_word_count(get_the_content()), -2); 
  13. $minutes_fast = ceil($wordcount / 250); 
  14. $minutes_slow = ceil($wordcount / 150); 
  15. if ($wordcount <= 150) { 
  16. $output = __("< 1 minute"); 
  17. else { 
  18. $output = sprintf(__("%s - %s minutes"), $minutes_fast, $minutes_slow); 
  19. echo $output; 
  20. endif; 
  21. if (!function_exists('est_the_content')): 
  22. function est_the_content( $orig ) { 
  23. // Prepend the reading time to the post content 
  24. return est_read_time(true) . "\n\n" . $orig; 
  25. endif; 
  26. // 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; 

再把調用統計代碼加到自己認為適合的位置。

經測試對中文統計沒有什么問題,英文統計的是字母。

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

效果看這篇文章標題下面信息欄

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产成人免费精品 | 视频一区二区中文字幕 | 欧美日韩激情 | 国产成人综合在线观看 | 久久国产一级片 | 亚洲网站免费观看 | 97中文字幕第一一一页 | 久久超 | 亚州欧美视频 | 黄色av网站免费 | 午夜爱爱福利 | 日韩视频一 | 一区二区三区视频在线 | 色阁五月 | 久久一区国产 | 日本欧美一区二区三区在线观看 | 一级网站 | 日本一区免费看 | 少妇一级淫片免费看 | 精品无吗乱吗av国产爱色 | 久久久久久久久久综合 | 欧美毛片在线观看 | 把娇妻调教成暴露狂 | 日本不卡一区二区三区在线观看 | 成人午夜在线播放 | 欧美成人性生活片 | 久久久久久久久久久av | 久久91久久久久麻豆精品 | 老司机免费福利午夜入口ae58 | 中国av免费在线观看 | 极品xxxx欧美一区二区 | 91九色视频观看 | 久久影库 | 99日韩精品视频 | 草莓视频久久 | 精品久久中文字幕 | 综合色视频 | 在线免费91| 在线天堂中文在线资源网 | 国产精品美女久久久久久不卡 | 黄色伊人网站 |