Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16000

How to display post summary on index page using Jekyll?

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

Answers (4)

Sam
Sam

Reputation: 515

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.

Upvotes: 36

KrauseFx
KrauseFx

Reputation: 11751

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

Upvotes: 3

Robin Zimmermann
Robin Zimmermann

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

Jostein
Jostein

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

Related Questions