How can I show just the most recent post on my home page with jekyll?

TemplatesJekyllTemplatingLiquidLiquid Layout

Templates Problem Overview


<ul class="entries">
  {% for post in paginator.posts %}
  <li>
    <a href="{{ post.url }}">
    <h3>{{ post.title }}</h3>
    <p class="blogdate">{{ post.date | date: "%d %B %Y" }}</p>
    <div>{{ post.content |truncatehtml | truncatewords: 60 }}</div>
    </a>
  </li>
  {% endfor %}
</ul>

That shows all my posts, I just want to show the most recent.

Templates Solutions


Solution 1 - Templates

This can be accomplished through the use of limit:

{% for post in site.posts limit:1 %}
... Show the post ...
{% endfor %}

You can also use limit and offset together to "feature" your most recent post:

<h1>Latest Post</h1>
{% for post in site.posts limit:1 %}
... Show the first post all big ...
{% endfor %}
<h1>Recent Posts</h1>
{% for post in site.posts offset:1 limit:2 %}
... Show the next two posts ...
{% endfor %}

Solution 2 - Templates

Rather than create a loop, just assign the variable and move on...

{% assign post = site.posts.first %}

(Edit 2018) Since someone wanted to know how to iterate other posts after you've done this:

{% for post in site.posts offset:1 %}
  ... Show the next posts ...
{% endfor %}

Solution 3 - Templates

If you got here for the question as stated in the title, "How can I show just the most recent post on my home page with jekyll?" and not "how do I show only the latest post in my template," the following might be helpful.

Given a brand new Jekyll version 3.7.3 install with the default minima theme, create a file, _layouts/home.html with the following content:

---
layout: none
---
{{ site.posts.first }}

Causes Jekyll 3.7.3 to show the first post, using the post template, as the home page.

Solution 4 - Templates

It appears you can also just access the latest post via the first index of site.posts as in:

{%- assign latest_post = site.posts[0] -%}

Latest post: <a href="{{ latest_post.url }}">{{ latest_post.title }}</a>

While site.posts.first works too as mentioned by someone else, the above example also provides a consistent manner for accessing other indices besides just the first (not that you would ever need to). Also, I didn't have enough reputation to add this answer as a comment instead :)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkabirView Question on Stackoverflow
Solution 1 - TemplatesDaniel BairdView Answer on Stackoverflow
Solution 2 - TemplatesMerovexView Answer on Stackoverflow
Solution 3 - TemplatesDouglas LovellView Answer on Stackoverflow
Solution 4 - TemplatesMatt BorjaView Answer on Stackoverflow