WordPress 文章字数统计代码

Gavin Wu 2023年01月17日 8:01 Wordpress 1,067 Views

将下面代码扔到主题的 functions.php 文件

1
2
3
4
5
6
7
8
9
//字数统计
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;
}
}

调用代码

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

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top