corazza
corazza

Reputation: 32354

How can I get a post excerpt in Jekyll?

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 %}`?

Upvotes: 52

Views: 24464

Answers (5)

Sentry.co
Sentry.co

Reputation: 5569

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 👌

Upvotes: 0

Tim Abell
Tim Abell

Reputation: 11881

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.

Upvotes: 6

mrpandey
mrpandey

Reputation: 1019

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.

Upvotes: 5

Brian Willis
Brian Willis

Reputation: 23854

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

Upvotes: 61

JMH
JMH

Reputation: 1289

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

Upvotes: 96

Related Questions