我们在WordPress主题开发的时候希望文章列表统计显示文章字数.
之前介绍了如何统计文章的图片数量:
WordPress主题开发:文章列表统计显示文章内图片数量
那么统计文章数量的功能.如下图这个功能,这个功能如何实现?
打开当前WordPress主题开发的functions.php文件,在<?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; } }
把调用统计代码加到自己认为适合的位置,如archive.php或者 single.php 文件的某个位置。
<?php echo count_words ($text); ?>
参考文章: 向阳WordPress主题开发教程二:WordPress主题模板文件结构说明