亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

為WordPress添加文章字數(shù)統(tǒng)計的方法

  發(fā)布時間:2012-10-06 16:22:08   作者:佚名   我要評論
下面看一下如何統(tǒng)計文章字數(shù)

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

一、把下面代碼加到主題的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中希望顯示字數(shù)統(tǒng)計的位置加上:

  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中希望顯示字數(shù)統(tǒng)計的位置加上:

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

可惜上述兩種方法對漢字統(tǒng)計無效,只適合純英文站點,網(wǎng)上也沒發(fā)現(xiàn)與中文博客字數(shù)統(tǒng)計相關的文章,沒辦法還是自己折騰一個吧。

WordPress中文博客文章字數(shù)統(tǒng)計代碼

[reply]

添加方法與上述相同,首先把下面代碼加到functions.php文件中。( 注:HotNews主題加到“//全部結(jié)束”前面 )

  1. //字數(shù)統(tǒng)計
  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. }

再把調(diào)用統(tǒng)計代碼加到自己認為適合的位置。

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

經(jīng)測試對中文統(tǒng)計沒有什么問題,英文統(tǒng)計的是字母。

[/reply]

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

相關文章

最新評論