0

我正在深入研究静态站点生成器并尝试使用基本的 110 博客构建一个博客: https ://github.com/11ty/eleventy-base-blog

到目前为止,一切都很好,但我想知道如何让它在主页/索引页面上显示最新的帖子。

开箱即用,它将显示 3 个使用 nunjucks 的帖子的最新链接:

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

但是我想知道如何从posts目录中获取最新的降价帖子,看看它是否可以拉出来渲染。

我认为必须有一种方法可以像这样检查 nunjucks 中的日期(我知道这是错误的,但试图得到一个想法):

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

无论如何,我知道这是可能的,但我仍在努力学习并寻求指向正确的方向。

4

1 回答 1

1

我认为这可能对你有用:

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

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

我基本上只是使用第一篇文章的 templateContent 变量。我在模板中将该 set 命令移到了更高的位置,这样我就可以使用另一个 H3。这是我的整个文件:

---
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>
于 2020-10-13T20:04:28.450 回答