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

时间:2014-09-09 18:17:43

标签: 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;

}

然后称之为

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

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

1 个答案:

答案 0 :(得分:4)

该函数返回一个值。你没有回复返回的值。

<?php echo bm_estimated_reading_time() ?>
相关问题