如何在 WordPress 博客文章页面添加文章字数和预计阅读时间?-经验交流论坛-轻友天地-轻论坛

如何在 WordPress 博客文章页面添加文章字数和预计阅读时间?

8d33ea7d39212600

有些人可能会想在自己的博客文章页面上添加文章字数和预计阅读时间,以便访问者可以更好地了解文章内容。虽然这种功能看起来很实用,但实际上它并不总是准确,也不能保证对所有读者都有用。

无论如何,如果你仍然有这个需求,那么在 WordPress 主题文章页面顶部添加这个信息的方法并不难。你只需要在 WordPress 的主题文件中添加一些代码,就可以实现这个功能了。

添加教程

1.在主题目录下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/300); // 修改数字300调整时间
  $output .= '共计' . $text_num . '字,阅读大约' . $read_time  . '分钟。';
  return $output;
}

2.你需要在主题中找到 single.php 或 content-single.php 这两个文件,它们通常是文章页面的模板文件。然后,你只需在文件中添加以下代码,即可。

<!--文章字数和阅读时间-->
<i class="fa fa-hourglass-start"></i> <?php echo count_words_read_time(); ?>

这段代码将获取文章的正文内容,计算出单词数量和预计阅读时间,并将它们显示在文章页面的顶部。当然,你可以根据自己的需要对样式和显示内容进行修改。

请登录后发表评论

    没有回复内容


Warning: error_log(/www/wwwroot/www.qingluntan.cn/wp-content/plugins/spider-analyser/#log/log-3022.txt): failed to open stream: No such file or directory in /www/wwwroot/www.qingluntan.cn/wp-content/plugins/spider-analyser/spider.class.php on line 2900