简体   繁体   中英

Get the read time for a specific post in Jekyll Pages from home page/ any other page

I am new to liquid and Jekyll, using the below code to calculate the post read time on the post page. But how should I calculate the reading time on the home page or any other page to display it below a post title?

When I use the same code on the homepage as well, I get different read times on the home page and inside that particular post.

I think I am not able to get the context of that post to count the number of words on some other page. Please suggest.

<span class="reading-time" title="Estimated read time">
  {% assign words = content | number_of_words %}
  {% if words < 360 %}
    1 min
  {% else %}
    {{ words | divided_by:180 }} mins
  {% endif %}
</span>

This is probably happening because, on the home page, you also have some HTML formatting your page.

Strictly speaking, you should indeed strip the HTML out of your content before counting the words, to have an accurate reading time.

So, using the strip_html filter could be a solution:

{% assign words = content | strip_html | number_of_words %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM