Add 'read more' only to posts that are longer than excerpt

hi jekyll land,

i use jekyll w minimal mistakes. i’m a bit clumsy with programming stuff (i’m not particularly liquid literate) but i have my own site running pretty ok now.

initially i had a problem where on my blog feed, excerpts of long posts looked the same as short posts, it wasn’t clear that you cd click on the long post to read more than the excerpt. so i added a “(read more)” thingy to it, like so:
{% if post.excerpt %}
<p><a href="{{ post.url }}">(more...)</a></p>
{% endif %}

but this appears on all posts, even the short ones where there’s no “more” to be read by clicking on the post.

surely there’s a way to make this conditional on the post being excerpted? is my “if post.excerpt” condition a dud?

i searched the docs and also this forum to no avail, perhaps not finding anything because i don’t know what this ‘read more’ function is actually called in blogland.

This check will always return true, because the page will always have an excerpt. Jekyll grabs the the first sentence/paragraph and stores it even if the page is blank, and liquid does not consider an empty string of nothing as a false.

What you need to do is check the post.content (I think its content? output? which ever one has the raw version, not the rendered html), and check its length (with .size) is greater than the length of post.excerpt.

So something like {% if post.content.size > post.excerpt.size %}

I’m not at a machine where I have jekyll to test the exactness of this code, but it should be something similar.

1 Like

thx uberfuzzy, instant success.

post.content worked for me.

thx for super fast solution, i never would have worked it out.

@mouseb I’m having a similar issue, the entirety of my posts are showing with a “Read More” button below - but of course, the read more is redundant.

How can I get the posts to autotmatically only show the excerpts and not the whole post? My title is like this:

---
title:  "Networks and Deep Learning Course with Andrew NG"
layout: post
categories: media
---
{% if post.content > post.excerpt.size %}
<p><a href="{{ post.url }}">(more...)</a></p>
{% endif %}

![Picture Classifier Network](/assets/Images/Network.png)

## Course Content

Now with some more…

did you try post.content.size? just post.content is not going to work.

I just setup my own blog and I was googling for exactly this problem. I have still one question - where did you add that code? Should I create a new file in the _layout directory (maybe customize a of copy minima/home.html at master · jekyll/minima · GitHub ?) or add it to some config file?

OK - I copied the home.html file edited it and it works :slight_smile:

1 Like