在Jekyll中制作基于页面的自定义循环

Pau*_*vis 13 liquid jekyll

我想创建另一个基于页面的循环,就像_posts文件夹适用于博客部分一样,但是对于一个小的杂志目录.(希望这很有意义)

也许我误解了一些简单的事情,但我无法解决这个问题.我有这个循环,感觉它应该工作,但没有任何东西返回.

{% for page in site.pressitems %}
<li>
    <a href="{{ post.url }}">{{ page.title }}</a>
</li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

代码,链接,解释,任何事情都非常感谢.:)

kik*_*ito 20

您无法site像这样添加自己的集合.

site只知道三个集合:pages,posts,和categories.您可以通过执行获取类别的所有帖子site.<category>.posts.AFAIK,类别仅适用于帖子,而不适用于页面.

这是有道理的,因为Jekyll应该主要是一个博客引擎,而不是一个通用的静态网站生成器.

所以你现在最好的解决方案就是"撒谎"到杰基尔.让它相信你有帖子,实际上你正在制作页面.

_posts/
  pressitems/
  blog/
Run Code Online (Sandbox Code Playgroud)

你将能够遍历_posts/pressitems中的元素,如下所示:

for item in site.categories.pressitems.posts do
  ... {{ item.title }} ... {{ item.url }}
endfor
Run Code Online (Sandbox Code Playgroud)

同样,您的"真实博客条目"将采用这种方式:

for p in site.categories.blog.posts do
  ... {{ p.title }} ... {{ p.url }}
endfor
Run Code Online (Sandbox Code Playgroud)

问题是你必须尊重Jekyll关于文件名的命名惯例; 你的pressitems必须看起来像真正的帖子.这意味着它们必须以yyyy-mm-dd字符串开头命名,如帖子.只要给他们一个随机的约会.

_posts/
  pressitems/
    1901-01-01-the-first-press-item.textile
    1902-01-01-the-second-one.textile
Run Code Online (Sandbox Code Playgroud)

编辑:这篇文章最初是在2012年编写的,但事实并非如此.Modern Jekyll允许您创建自己的收藏集https://jekyllrb.com/docs/collections/


bat*_*lix 15

你可以迭代 site.pages

{% for page in site.pages %}
  <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
  <p>{{ page.content }}</p>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

并限制列表使用特定布局的唯一页面.

{% for page in site.pages %}
  {% if page.layout == 'team' %}  
    <h3><a href="{{ page.url }}">{{ page.title }}</a></h3>
    <p>{{ page.content }}</p>
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

请参阅有关生成站点地图的帖子:http: //vvv.tobiassjosten.net/jekyll/jekyll-sitemap-without-plugins/


dav*_*man 11

在Jekyll 2.5.3中,您实际上可以将自己的集合添加到站点.

将_my_collection文件夹添加到root并填充文档.添加到_config.yml:collections: - my_collection

现在使用帖子,页面或类别调用文档.例如 October 2016

值得注意的是,可以使用此功能,它已被Jekyll团队标记为"实验性功能,API可能会更改,直到功能稳定".


TKo*_*KoL 5

在 jekyll 上,您还可以向页面添加 yaml front-matter。添加自定义前台内容并没有错,例如页面类别。

---
layout: plain
title: "My beautiful page"
description: ""
snippet: ""
page-category: "category 1"
---
Run Code Online (Sandbox Code Playgroud)

通过以下方式访问它们:

{% for page in site.pages %}
   {% if page.page-category == "category 1" %}
        {{ page.content }}
   {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)