Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
truncate blog posts in jekyll
<!-- using the truncate filter -->
{% for post in site.posts limit:10 %}
<h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
<span class="post-date">{{ post.date | date: "%B %d, %Y" }}</span>
{% if post.content.size > 2000 %}
{{ post.content | truncatewords: 300 }} <!-- bad! content gives you rendered html and you will truncate in the middle of a node -->
<a href="{{ post.url }}">read more</a>
{% else %}
{{ post.content }}
{% endif %}
<hr>
{% endfor %}
<!-- using the split filter -->
{% for post in site.posts limit:10 %}
<div class="post-preview">
<h2><a href="{{ post.url }}">{{ post.title }}</a></h2>
<span class="post-date">{{ post.date | date: "%B %d, %Y" }}</span>
{{ post.content | split:'<!--break-->' | first }}
{% if post.content contains '<!--break-->' %}
<a href="{{ post.url }}">read more</a>
{% endif %}
</div>
<hr>
{% endfor %}
<!-- In your posts file, put your marker wherever you want to cut off the post for the main blog page -->
---
layout: post
title: truncate example
---
Paragraph 1
Paragraph 2
<!--break-->
Paragraph 3
Paragraph 4
@liudangyi

This comment has been minimized.

Copy link

@liudangyi liudangyi commented Jun 15, 2013

Great!

@wojtekerbetowski

This comment has been minimized.

Copy link

@wojtekerbetowski wojtekerbetowski commented Sep 12, 2013

Cool, thanks

@jebbles

This comment has been minimized.

Copy link

@jebbles jebbles commented Jun 6, 2014

This Gist was super helpful, thanks! But as I was implementing your solution, I realized truncation may still be used viably using this method:

{{ post.content | strip_html | truncatewords: 300 }}

But this is assuming you're okay with removing paragraph structure and such and want to spit out plain-text.

@tsjensen

This comment has been minimized.

Copy link

@tsjensen tsjensen commented Jan 16, 2015

Just what I needed. Thanks!

@fehwalker

This comment has been minimized.

Copy link

@fehwalker fehwalker commented Feb 2, 2015

Useful, thanks!

@recio-sjogren

This comment has been minimized.

Copy link

@recio-sjogren recio-sjogren commented Apr 14, 2015

Thanks! Very useful!

@hardware

This comment has been minimized.

Copy link

@hardware hardware commented Aug 10, 2015

Great, Thanks !

@danilao

This comment has been minimized.

Copy link

@danilao danilao commented Sep 8, 2015

Amazing! Thanks!

@WizKIDz

This comment has been minimized.

Copy link

@WizKIDz WizKIDz commented Nov 24, 2015

very nice 👍

@Joozo

This comment has been minimized.

Copy link

@Joozo Joozo commented Nov 27, 2015

Good. then, you can do this:

---
excerpt_separator: <!--more-->

---

Excerpt
<!--more-->
Out-of-excerpt
// Need remove the <p> tags.
{{ post.excerpt | remove: '<p>' | remove: '</p>' }}
@dotmilk

This comment has been minimized.

Copy link

@dotmilk dotmilk commented Sep 6, 2016

{{ post.content | strip_html | truncatewords: 140 }}

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Dec 23, 2016

Thanks for this post! I'm just in trouble like this.

@basanthrzt

This comment has been minimized.

Copy link

@basanthrzt basanthrzt commented Jul 2, 2017

Awesome!

@ZeroSpree

This comment has been minimized.

Copy link

@ZeroSpree ZeroSpree commented Jul 16, 2017

Just what I needed, thank you!

@theimgclist

This comment has been minimized.

Copy link

@theimgclist theimgclist commented Mar 9, 2018

Thank you so much. This really helped!!

@bcetin

This comment has been minimized.

Copy link

@bcetin bcetin commented May 11, 2018

Really useful!

@koenidv

This comment has been minimized.

Copy link

@koenidv koenidv commented Jul 11, 2018

Thank you, simple yet very useful!

@ryoxxyz

This comment has been minimized.

Copy link

@ryoxxyz ryoxxyz commented Aug 28, 2019

Thank you so much! Useful!

@blu3mangroup

This comment has been minimized.

Copy link

@blu3mangroup blu3mangroup commented May 6, 2020

{{ post.content | strip_html | truncatewords: 300 }}

Useful, thanks.

@hahwul

This comment has been minimized.

Copy link

@hahwul hahwul commented Oct 20, 2020

Oh, thank you 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment