How can I get a post excerpt in Jekyll?
Asked Answered
D

5

52

I'm creating a new blog using Jekyll.

On the main page, there will be a list of my 10 most recent posts.

The entries on this list will include a title, the post date, and an excerpt, most likely the first paragraph.

I'm only familiar with using Jekyll for basic templates, so I can either put only a variable in the page, or include the entire post.

Is there a way to somehow avoid using post.content in the paginator, and only include up to a certain point in the post, which I define (e.g. ``{% endexcerpt %}`?

Dukey answered 15/10, 2013 at 19:28 Comment(1)
Also see this answer.Pyrethrum
G
61

Sure, you can use {{ post.excerpt }} in place of {{ post.content }}.

You can also manually override the automatically generated excerpts if you don't like them.

Full documentation on how to do this here: http://jekyllrb.com/docs/posts/#post-excerpts

Gallager answered 16/10, 2013 at 8:31 Comment(1)
Remember to use <!--more--> in the actual post to tell Jekyll where it should 'cut' the post for the excerpt content or set excerpt_separator globally in your _config.yml.Lawley
M
96

Something like {{ post.content | strip_html | truncatewords: 50 }} produces a more consistent excerpt. It gets the first 50 words and strips any formatting.

Murmurous answered 6/5, 2014 at 21:7 Comment(7)
Agreed. This is much better. I used to use excerpt and it would give all kinds of quirks about where it breaks (seemingly only on the first paragraph).Heisenberg
How would you deal with links though? For example, if you have content like this "This is a post. And a [link](www.link.com)", then the snippet you posted would display the entire raw text rather than being intelligent about it and showing link simply as text.Endogen
Got the answer, use {{ post.content | markdownify | strip_html | truncatewords: 50 }}Endogen
How to get post.content but without post.title? I don't want title in post excerpt.Scout
Hi, I found that this sentence could only be used in English, When I want to write a Chinese article, how can I use this function? Thanks!Shotwell
You can use truncate: 50 for the first 50 characters. I'm not sure how well this works with Chinese or other Utf-8 sequences without an example, though. post.excerpt should probably be improved at some point, I'd personally much rather use a plugin to do that, than stringing together a bunch of liquid filters. shopify.github.io/liquid/filters/truncateMurmurous
truncate: 50 truncates the first 50 characters but it is not consistent. For my Chinese posts I usually use this generic method, and provide an override if the formatting doesn't work out well by default.Criticism
G
61

Sure, you can use {{ post.excerpt }} in place of {{ post.content }}.

You can also manually override the automatically generated excerpts if you don't like them.

Full documentation on how to do this here: http://jekyllrb.com/docs/posts/#post-excerpts

Gallager answered 16/10, 2013 at 8:31 Comment(1)
Remember to use <!--more--> in the actual post to tell Jekyll where it should 'cut' the post for the excerpt content or set excerpt_separator globally in your _config.yml.Lawley
E
6

Use

 {{ post.content | markdownify | strip_html | truncatewords: 50 }}

instead of {{ post.excerpt }} or {{ post.content }}.

This will give consistent length blocks of unformatted text with no raw markdown content in them. Tidy.


Thanks to this comment by @karolis-ramanauskas for the answer, I've made it a proper answer so it can get better visibility.

Exasperation answered 9/4, 2020 at 10:9 Comment(0)
S
5

To get a custom length excerpt for each post, you can add a excerpt_separator variable in front matter of your post. If you set this variable to <!--end_excerpt-->, then post.excerpt will include all content before <!--end_excerpt-->.

---
excerpt_separator: <!--end_excerpt-->
---

This is included in excerpts.

This is also included in excerpts.

<!--end_excerpt-->

But this is not.

To save yourself the effort of adding excerpt_separator to front matter of each post, you can simply set it in _config.yml.

Stirpiculture answered 23/10, 2019 at 7:27 Comment(0)
R
0

What worked for me was: add this to the config.yml : excerpt_separator: "<!--more-->" #global excerpt separator for the blog posts and remember to restart the jekyll local server. All changes to config.yml requires a restart to take effect 👌

Reconcile answered 18/10, 2020 at 11:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.