探索,發現,愛好、學習,記錄,分享。
學海無涯,天涯若比鄰,三人行,必有我師。

計算文章的閱讀時間

在functions.php合適位置中置入以下代碼,函数1是获取文章字数,函数2是计算阅读的时间,數值60是閱讀的速度。

function count_words () {
    global $post;
    $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;

}

function read_time( $return = false) {
    $wordcount = round(count_words(), -2);
    $minutes = ceil($wordcount / 60);
    
    if ($wordcount <= 150) {
        $output = '1 min read';
    } else {
    $output = $minutes.' mins read';
}
echo $output;
    
}
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權
轉載事宜:如需轉載需徵得應允,轉載必須注明來源於本站的信息。
文章名称:《計算文章的閱讀時間》
文章链接:https://www.thefreesky.com/blog/10334.html
本站資源僅供個人學習交流,請於下載後24小時內刪除,不允許用於商業用途,否則法律問題自行承擔。

評論 抢沙发