为WordPress添加文章字数统计的方法

2019-02-19 16:53:30王冬梅

WordPress中文博客文章字数统计代码

[reply]

添加方法与上述相同,首先把下面代码加到functions.php文件中。( 注:HotNews主题加到“//全部结束”前面 )

  1. //字数统计 function count_words ($text) {
  2. global $post; if ( '' == $text ) {
  3. $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') . '个字';
  4. return $output; }
  5. }

再把调用统计代码加到自己认为适合的位置。

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

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

[/reply]

效果看这篇文章标题下面信息栏