有的小伙伴可能需要在網站中添加用戶閱讀文章時長的功能,對此,可以分析出用戶更喜歡哪種類型的文章。
添加一下代碼到當前主題目錄下的functions.php文件中:
// 添加閱讀時長功能
function get_reading_time($content) {
$sixe_insert_element = '<span>閱讀時長:%min%分%sec%秒</span>';
$sixe_per_min_characters = 300;
$sixe_insert_element = str_replace('%num%', $sixe_per_min_characters, $sixe_insert_element);
$words = mb_strlen(preg_replace('/\s/','',html_entity_decode(strip_tags($content))),'UTF-8');
$minutes = floor($words / $sixe_per_min_characters);
$seconds = floor($words % $sixe_per_min_characters / ($sixe_per_min_characters / 60));
return str_replace('%sec%', $seconds, str_replace('%min%', $minutes, $sixe_insert_element));
}
function sixe_reading_time() {
echo get_reading_time(get_the_content());
}
前臺調用標簽:<?php sixe_reading_time(); ?>
當前主題下的functions.php添加代碼之后,只需要在前臺使用標簽調用就可以了!





