1

I'm diving into the static site generators and trying to build a blog out using the basic eleventy blog: https://github.com/11ty/eleventy-base-blog

So far, everything is good, but I'm wondering how to go about to get it to show the latest post on the home/index page.

Right out of the box, it will show the 3 latest links to the posts using nunjucks:

{% set postslist = collections.posts | head(-3) %}
{% set postslistCounter = collections.posts | length %}
{% include "postslist.njk" %}

But what I'm wondering how to get the latest markdown post from the posts directory and see if it can pull that to render.

I am thinking that there's got to be a way to check the date within nunjucks like this (I know this is wrong, but trying to get an idea):

{% if post.date = currdate %}
{% include "posts.njk" %}
{% endif %}

Anyways, I know it's possible, but I'm still trying to learn and looking to get pointed in the right direction.

ultraloveninja
  • 1,969
  • 5
  • 27
  • 56

2 Answers2

2

I think this could work for you:

{% set postslist = collections.posts | head(-3) %}

<h1>Latest Post</h1>
{{ postslist[0].templateContent | safe }}

I basically just use the templateContent variable of the first post. I moved that set command higher in the template so I could use another H3. Here's my entire file:

---
layout: layouts/home.njk
eleventyNavigation:
  key: Home
  order: 1
---

{% set postslist = collections.posts | head(-3) %}

<h1>Latest Post</h1>
{{ postslist[0].templateContent | safe }}

<h1>Latest 3 Posts</h1>

{% set postslistCounter = collections.posts | length %}
{% include "postslist.njk" %}

<p>More posts can be found in <a href="{{ '/posts/' | url }}">the archive</a>.</p>
Raymond Camden
  • 10,661
  • 3
  • 34
  • 68
  • Awesome, thanks! Sorry for the delay. Been busy and finally getting around to this. I'll mess around with this today. – ultraloveninja Oct 16 '20 at 13:04
  • 1
    No worries - enjoy. (And if it works, accept the answer please. :) – Raymond Camden Oct 16 '20 at 17:29
  • I was able to get this to work by doing this: https://github.com/11ty/eleventy-base-blog/issues/78#issuecomment-712120241 Looks like it was a combination of both, but for the most part, it was the `{% set postslist = collections.posts | head(-3) %}` that did the trick. Thanks! – ultraloveninja Oct 19 '20 at 12:22
0

The accepted answer is outdated, because Nunjucks removed the built-in head filter. You can use first filter to select the most recent blog post like this:

{% set mostRecent = collections.posts | first %}

To select the first (or last) n posts, loop over collections.posts:

{% set top = 3 %}
{% for post in collections.posts %}
  {# ensure that top does not exceed the length of the collection #}
  {% if (loop.index <= top) and (top <= loop.length) %}
    <div>{{ post.title }}</div>
  {% endif %}
{% endfor %}

Use the reverse filter for the other direction.

See also: http://mozilla.github.io/nunjucks/templating.html#for

Stefan
  • 115
  • 2
  • 7