如何在wordpress中实现估计阅读时间功能


How can i implement an estimated reading time feature in wordpress?

我正试图将估计的阅读时间集成到wordpress主题中,但我似乎无法实现。我从这里拿走了代码http://wptavern.com/estimated-time-to-read-this-post-eternity。我把它粘贴到functions.php 中

function bm_estimated_reading_time() {
    $post = get_post();
    $words = str_word_count( strip_tags( $post->post_content ) );
    $minutes = floor( $words / 120 );
    $seconds = floor( $words % 120 / ( 120 / 60 ) );
    if ( 1 < = $minutes ) {
        $estimated_time = $minutes . ' minute' . ($minutes == 1 ? '' : 's') . ', ' . $seconds . ' second' . ($seconds == 1 ? '' : 's');
    } else {
        $estimated_time = $seconds . ' second' . ($seconds == 1 ? '' : 's');
    }
    return $estimated_time;
}

然后称之为

<p class="ert"><?php bm_estimated_reading_time() ?></p> 

在content-single.php中,就在author链接之后,没有显示任何内容。如果我用chrome查看帖子,我可以看到这段文字,但它是空的。我做错了什么,或者我还应该做什么?

函数返回一个值。您没有回显返回的值。
<?php echo bm_estimated_reading_time() ?>