62
<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.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
kabir
  • 747
  • 2
  • 6
  • 6

4 Answers4

124

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 %}
TMan
  • 1,775
  • 1
  • 14
  • 26
Daniel Baird
  • 2,239
  • 1
  • 18
  • 24
  • This really helped me, thanks. However in the first example i had to put a value for offset otherwise the first post wouldn't show up. Like: {% for post in site.posts offset:1 limit:1 %}. Maybe it changed in the recent versions of Jekyll. – Christian Dec 30 '13 at 15:07
  • Christian are you iterating through paginator or site to get your posts? Maybe the paginator is 1-indexed or something. – Daniel Baird Jan 07 '14 at 01:35
  • No, sorry. I was exactly using: {% for post in site.posts limit:1 %} – Christian Jan 07 '14 at 07:17
  • Must be a version change then. Actually that makes sense given the big update Jekyll had during 2013. I'll dink around and update this answer.. – Daniel Baird Jan 09 '14 at 03:35
  • 2
    Side note, for accessibility reasons you only want one `

    ` tag per page. Awesome answer tho, thank you! Very helpful.

    – Freedom_Ben Jun 30 '16 at 06:42
  • @Freedom_Ben Perhaps use sectioning tags then use H2 as a higher-level sub-topic of the outline. – Denver Prophit Jr. May 22 '19 at 10:43
19

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 %}
Merovex
  • 921
  • 9
  • 10
  • This works if you only want to show one post, but it interferes with being able to list the rest of the posts on the same page. – Jeremy_inPDX Jan 19 '18 at 19:38
  • The question asked was "I want to show only one post," and my response answers it. If you later want to show other posts, then you create a loop of the site.posts offsetting by one. This lets you start on post #2. The other answer shows the offset, in case you're not familiar. – Merovex Jan 20 '18 at 20:03
9

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.

Douglas Lovell
  • 1,549
  • 17
  • 27
  • 1
    This should be `layout: null`, not `none`. – Soviut Sep 02 '18 at 20:10
  • This is the best solution for me because it seems to preserve things like page.url and other variables that get lost in the other solutions. This means you can use the same layout page as your other post pages. – zola25 Apr 30 '21 at 20:07
2

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 :)

Matt Borja
  • 1,509
  • 1
  • 17
  • 38