Jekyll 按月发布的帖子

col*_*all 2 archive jekyll

我正在尝试从我的网站创建帖子的存档页面。我希望能够以这种格式按月为每个帖子列表提供一个页面:

www.mywebsite.com/2016/11 将显示 2016 年 11 月的所有帖子。

我是否可以为我发布的每个月创建一个页面,每次我在新月份发布帖子时都会动态创建该页面?我不想每个月手动创建一个新页面。

我已经可以按年份对帖子进行分组,如下所示:

<ul>
{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%Y" %}
  {% if currentdate != date %}
    <li id="y{{currentdate}}">{{ currentdate }}</li>
    {% assign date = currentdate %} 
  {% endif %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。

was*_*ful 5

您可以修改date过滤器以集成月份,例如date: "%B %Y"。这就是我在布局中使用的,<ul>每个月都有一个单独的布局。

根据文档,过滤器的仅月份值date是:

  • %b: 月份名称缩写。
  • %B:完整的月份名称。
  • %m:一年中的月份 (01 - 12)。

完整循环:

<ul>
{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%B %Y" %}
  {% if currentdate != date %}
    <li id="y{{currentdate}}">{{ currentdate }}</li>
    {% assign date = currentdate %} 
  {% endif %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

关于生成页面,据我所知,这只能通过插件来完成。如果您无法使用插件,例如,如果您在 GitHub 上托管页面,那么您能做的最好的事情就是根据布局将页面缩减为简短的 YAML frontmatter,如本答案中所示。