tom_mai78101 tom_mai78101 - 6 months ago 24
HTML Question

Obtaining the word count from a post in Jekyll for Github Pages?

Here's the code snippet that I'm currently using to see if a blog post exceeds over 120 words. If true, truncate the content and add a "READ MORE" link at the bottom of the post.

<div class="blogs">
{% for post in site.posts %}
<article class="post">
<h3><a href="{{ site.baseurl }}/{{ site.blogs }}{{ post.title }}">{{ post.title }}</a></h3>

{% assign wordCount = {{ post.content | size }} %}
{% if wordCount > 120 %}
<div class="entry">
{{ post.content | truncatewords:120}}

<a href="{{ site.baseurl }}{{ post.url}}" class="read-more">Read More</a>
{% else %}
{{ post.content }}
{% endif %}
{% endfor %}

When I commit this, I received an email from GitHub saying that there's a page error upon building, and nothing else was printed out.

I'm guessing it has to do with
{{ post.content }}
not being able to filter out the size, so I'm stuck.

How would I obtain the word count of individual blog posts, so that I'm able to truncate some of the posts with over 120 words in them? Thanks in advance.


{{ post.content | size }} gives you the number of characters in post.content string.

If you want to count word in a string, use number_of_words liquid filter.

{% assign wordCount = post.content | number_of_words %}

Note : you have a strange link in the post title {{ site.baseurl }}/{{ site.blogs }}{{ post.title }} I think it's {{ site.baseurl }}{{ post.url}} like in the read more link.