让WordPress的文章页显示字数统计

2020年07月24日 1933点热度 4人点赞 0条评论

WordPress的文章页一般会显示热度、点赞、评论统计这几项,当我们想要让文章的字数统计也显示在文章页上方该怎么办呢?下面大头就教大家用代码来给WordPress网站文章添加显示字数统计功能。

让WordPress的文章页显示字数统计

让WordPress的文章页显示字数统计方法步骤:

  1. 登录WordPress后台,并进入主题编辑器。
  2. 将下方的让WordPress文章显示字数的代码复制到主题编辑器中的functions.php文件中。
  3. 接着将调用代码复制到single.php(文章页面文件)想要让他显示的地方即可。

WordPress文章显示字数统计功能代码:

// 字数统计代码
function zm_count_words ($text) {
    global $post;
    if ( '' == $text ) {
        $text = $post->post_content;
        if (mb_strlen($output, 'UTF-8') < mb_strlen($text, 'UTF-8')) $output .= '<span class="word-count">共' . mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($post->post_content))),'UTF-8') .'字</span>';
        return $output;
    }
}
//调用代码
<?php echo zm_count_words($text); ?>

希望这篇文章可以帮助你让你的WordPress网站文章成功显示字数统计。

相关文章:

  1. WordPress发布文章自动添加标签
  2. WordPress添加文章显示百度是否收录功能

大头

勤而自律,心似明镜。