核心代码
将下面代码复制到 wp 主题的 functions.php 文件里:
//字数统计
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); ?>
放在文章配置文件(合适的位置就可以了)测试中文统计正常,英文统计字母。
![图片[1]-WordPress添加网站文章内容页字数统计代码-AA源码网 | 源码收藏](https://images.aaym.net/2021/11/1636334585-df28425fe83e29ab90f5310fdac52cb2.jpg!/format/webp/lossless/true)
本文结束END

© 版权声明
THE END
暂无评论内容