有些文字站的站长不妨使用一下本次教程,WordPress计算文章阅读时间,这样就可以计算一下一篇文章的所需阅读时间了。不过这是估算,每个人的阅读快慢都不同,是不可能兼顾到每个人的。

functions.php中加入代码

/*
WordPress计算文章阅读时间
http://www.wpxzt.com
*/
function fa_get_postlength(){
    global $post;
    return strlen( strip_shortcodes(strip_tags(apply_filters('the_content', $post->post_content))) );
}
function fa_get_post_img_count(){
    global $post;
    preg_match_all('//sim', $post->post_content, $strResult, PREG_PATTERN_ORDER);
    return count($strResult[1]);
}
function fa_get_post_readtime(){
    global $post;
    return ceil(fa_get_postlength() / 800 + fa_get_post_img_count() * 8 / 60);
}

调用的话就使用下代码,请务必在循环中使用:

<?php echo fa_get_post_readtime(); ?>

参与评论