21

I am using Jekyll to create a blog by following this excellent tutorial. I would like to add a post summary on the index page. I tried using:

post.content | truncatewords:50 | strip_html

it works but it displays the entire post until the 50 word count is reached. This includes the heading too. I would like to just summarize the actual content of the post. How can I structure my posts to do this?

4 Answers 4

36

Update 16 Nov, 2015

Now Jekyll support excerpt separator, In template you can do this:

{% if post.excerpt %}
    {{ post.excerpt }}
{% endif %}

and In global config _config.yml you can set:

excerpt_separator: <!--more-->

and the same use with <!--more--> html comment tag.

Old answer

You can try this:

{% if post.content contains '<!--more-->' %}
  {{ post.content | split:'<!--more-->' | first }}
{% else %}
  {{ post.content }}
{% endif %}

and add <!--more--> tag in the article after summary, just like Wordpress.

1
  • 2
    It's better to combile all above solutions: {% else %} {{ post.content | truncatewords:50 | strip_html }} {% endif %}
    – Evans Y.
    Jan 29, 2014 at 7:49
5

From the Jekyll documentation:

Each post automatically takes the first block of text, from the beginning of the content to the first occurrence of excerpt_separator, and sets it as the post.excerpt.

...

Because Jekyll grabs the first paragraph you will not need to wrap the excerpt in p tags, which is already done for you.

See http://jekyllrb.com/docs/posts/#post-excerpts for more info and an example.

1
  • The link really helped! Thanks
    – Gunith D
    Nov 6, 2016 at 7:23
4

Use YAML front matter and define a separate title per post, like this:

---
title: Efficient smuflet based kwoxel trees
---

Post content goes here.

Then you can use or not use post.title as you please.

Or, if you want to write a separate summary (not just the first n characters) for each post, just add a field for that summary in the front matter as well.

2
  • Nice idea about a summary field.It is more flexible to add a summary myself than truncating the existing post.I should try that out. Mar 19, 2013 at 11:09
  • 1
    Latest beta version has better support for excerpts. Apr 11, 2013 at 23:30
3

Use {{ post.excerpt }} in your index.md file to get an excerpt of this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.