如何在Wordpress中实现估算的阅读时间功能?

Cri*_*n N 3 php 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;

}
Run Code Online (Sandbox Code Playgroud)

然后叫它

<p class="ert"><?php bm_estimated_reading_time() ?></p> 
Run Code Online (Sandbox Code Playgroud)

在content-single.php中,在作者链接之后,没有任何显示。如果我查看Chrome中的帖子,则可以看到该段落,但该段落为空。我做错了什么,还是应该做什么呢?

Pop*_*les 5

该函数返回一个值。您没有在回显返回的值。

<?php echo bm_estimated_reading_time() ?>
Run Code Online (Sandbox Code Playgroud)