【wordpress代码】给博客添加文章字数统计代码

代码实现前台文章界面显示文章字数统计代码 之前只能在后台显示,现在前台也就可以显示了

使用方法

functions.php文件中加入以下代码-有些主题会是在functions-theme.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); ?>

提示:放在文章配置文件(合适的位置就可以了)

发表评论