在一篇文章的開頭或是結尾放上一個文章總字數提示,就可以幫助你的訪客大概的了解這篇文章了。這次的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;
}
}
在需要展示的地方添加下列代碼:
<?php echo count_words ($text); ?>
效果:

既然字數都統計了,就加個預計讀完的時間吧。