How do I get the content from my articles?

I am using the following HTML code to find the estimated reading time for my blog posts:

<abbr title="Estimated reading time">ERT {{ page.title }}</abbr>

{% assign words_total = page.content | replace: '<script type="math/tex">', '' | replace: '<script type="math/tex; mode=display">', '' | replace: '</script>', '' | strip_html | number_of_words %}

{% assign words_without_code = page.content | replace: '<pre class="highlight">', '<!--' | replace: '</pre>', '-->' | replace: '<script type="math/tex">', '' | replace: '<script type="math/tex; mode=display">', '' | replace: '</script>', '' | strip_html | number_of_words %}

{% assign words_without_math = page.content | strip_html | number_of_words %}

{% assign words_without_either = page.content | replace: '<pre class="highlight">', '<!--' | replace: '</pre>', '-->' | strip_html | number_of_words %}

{% assign words_code = words_total | minus: words_without_code | divided_by: 2.0 %}

{% assign words_math = words_total | minus: words_without_math | times: 2.0 %}

{% assign words = words_without_either | plus: words_code | plus: words_math | round %}

{% assign ert = words | divided_by:250 | at_least: 1 %}

{{ ert }} minute{% if ert != 1 %}s{% endif %}

I want to show the reading time on my home page next to my date. However, the script shows me different reading times in the home page, and in the actual blog post. I reckon it’s not reading the article content but just the home page. Any ideas how to go about this? I’m kinda new at this whole thing.

what does the loop on your home page look like? you must be looping over each post there, inside that loop you need to put that code more or less. But it won’t be page.content it will be whatever you call each item in the loop .content.

For example I usually do

for item in site.posts
//add your code in here and use item.content not page.content (and item.title)
1 Like

Thanks alot!! You’re a lifesaver. Let me tell you how I solved it:

{% if page.url == "/" %}
    {% assign read_content = _article.content %}
{% else %}
    {% assign read_content = page.content %}
{% endif %}

So I basically set it to use page.content if it’s not on the home page, and _article.content on the home page.

Also, anyone else who’s gonna try something similar should note that this doesn’t work properly with incremental build. Hope it helps someone else!!