以前写文章常常看字数,wordpress后台用了古登堡后,后台看不到字数统计了,索性在前台添加一个字数统计。
网上找来的代码。在functions.php里添加:
//字数和预计阅读时间统计
function count_words_read_time () {
global $post;
$text_num = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
$read_time = ceil($text_num/400);
$output .= '本文《' . get_the_title() .'》共' . $text_num . '个字,系统预计阅读时间或需' . $read_time . '分钟。';
return $output;
}
其中400是每分钟阅读字数,可以修改。
做了一下修改:
//字数和预计阅读时间统计
function count_words_read_time () {
global $post;
$text_num = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8');
$read_time = ceil($text_num/400);
$output .= '共' . $text_num . '个字,预计阅读需' . $read_time . '分钟';
return $output;
}
在合适位置添加:
<?php echo count_words_read_time(); ?>
每个主题位置会有不同,本站用的cosy在post-mate文件中,我将其放在了作者信息后,效果如下:

请登录以参与评论
现在登录