WordPress在后盾编辑日志时编辑框左下角有一个字数统计,不过只显示在后盾,能不能在前台也加上文章字数统计性能呢?钻研了一下顺序源文件,发现中文版WP后盾的字数统计性能,是经过wp-content\languages目录的zh_CN-word-count.js完成的,就是不晓得如何调用。网上搜了一下,找到两篇老外给出的代码:
一、把上面代码加到主题的functions.php文件中:
- function count_words($str){
- $words = 0;
- $str = eregi_replace(" +", " ", $str);
- $array = explode(" ", $str);
- for($i=0;$i < count($array);$i++)
- {
- if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
- $words++;
- }
- return $words;
- }
而后在single.php中心愿显示字数统计的地位加上:
- Word count: <?php echo count_words($post->post_content); ?>
原文
二、还是将上面代码加到functions.php文件中,此办法与下面不同的是,还加上了一个预算的浏览工夫:
- function show_post_word_count(){
- ob_start();
- the_content();
- $content = ob_get_clean();
- return sizeof(explode(" ", $content));
- }
- if (!function_exists('est_read_time')):
- function est_read_time( $return = false) {
- $wordcount = round(str_word_count(get_the_content()), -2);
- $minutes_fast = ceil($wordcount / 250);
- $minutes_slow = ceil($wordcount / 150);
- if ($wordcount <= 150) {
- $output = __("< 1 minute");
- } else {
- $output = sprintf(__("%s - %s minutes"), $minutes_fast, $minutes_slow);
- }
- echo $output;
- }
- endif;
- if (!function_exists('est_the_content')):
- function est_the_content( $orig ) {
- return est_read_time(true) . "\n\n" . $orig;
- }
- endif;
-
同样在single.php中心愿显示字数统计的地位加上:
- The following <?php echo show_post_word_count(); ?> words should take about <?php echo est_read_time(); ?> to read.
惋惜上述两种办法对汉字统计有效,只适宜纯英文站点,网上也没发现与中文博客字数统计相干的文章,没方法还是本人折腾一个吧。
WordPress中文博客文章字数统计代码
[reply]
增加办法与上述相反,首先把上面代码加到functions.php文件中。( 注:HotNews主题加到“//全副完结”后面 )
- function count_words ($text) {
- global $post;
- if ( '' == $text ) {
- $text = $post->post_content;
- 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') . '个字';
- return $output;
- }
- }
再把调用统计代码加到本人以为适宜的地位。
- <?php echo count_words ($text); ?>
经测试对中文统计没有什么成绩,英文统计的是字母。
[/reply]
成果看这篇文章题目上面信息栏
以上就是安达网络工作室关于《为WordPress添加文章字数统计的方法》的一些看法。更多内容请查看本栏目更多内容!