Reputation: 16000
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?
Upvotes: 21
Views: 9149
Reputation: 515
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.
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.
Upvotes: 36
Reputation: 11751
Use {{ post.excerpt }}
in your index.md
file to get an excerpt of this post.
Upvotes: 3
Reputation: 3884
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.
Upvotes: 5
Reputation: 3144
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.
Upvotes: 4