static-site - 使用 Eleventy 获取要在主页上显示的最新帖子
问题描述
我正在深入研究静态站点生成器并尝试使用基本的 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 %}
无论如何,我知道这是可能的,但我仍在努力学习并寻求指向正确的方向。
解决方案
我认为这可能对你有用:
{% 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>
推荐阅读
- ios - 同时使用两个 AVSpeechSynthesizer 实例
- powershell - 如何在 Powershell 中读取桌面通知
- c++ - 如何从 C++ 中的集合中删除一个项目而不删除它?
- typescript - 短路变量赋值 已知至少定义了一个变量
- java - Java 加密比较:SunJCE 是否使用本机代码?
- c++ - 如何更改 WebBrowser 全屏视频模式?
- aws-iot - AWS IoT Greengrass 与 FreeRTOS
- r - 如何在R中选择性地定位y轴上的重叠点
- django - 运行 PyCharm 测试时如何解决“django.core.exceptions.ImproperlyConfigured:找不到 GDAL 库”?
- ios - iOS AlertController 背景的默认不透明度是多少?