如何在Jekyll中定义自定义集合?

时间:2014-06-17 01:11:50

标签: jekyll liquid github-pages jekyll-bootstrap

Jekyll Collections documentation之后,我在_config.yml

中编写了以下代码
_config.yml

collections: 
- popular_posts

因此,当我打印{{site.collections}}时,输出为" popular_posts"。

我还创建了一个名为" _popular_posts"的文件夹。与" _posts"处于同一水平。 _popular_posts包含两个.md文件,其中包含一些YAML前端内容,与帖子相同。

但是,如果我打印{{site.popular_posts}}或{{site.collections.popular_posts}},则没有输出。

如何让Jekyll识别该目录中的.md文件,以便以下代码能够正常工作?

{% for popular_post in site.popular_posts %}
  <a href="{{ popular_post.link }}">
    <h1>{{ popular_post.title }}</h1>
    <img class="pop-img" src="{{ popular_post.image_url }}">
  </a>
  <span id="pop-order"><span class="pop-current-popular_post-number">{{ popular_post.number }}</span>/5</span>
{% endfor %}

1 个答案:

答案 0 :(得分:5)

这很容易!你走在正确的轨道上。在_config.yml

collections:
- popular_posts

这将告诉Jekyll读入_popular_posts中的所有文件。

如果您希望这两个文件中的每一个都有相应的输出文件(例如_posts现在的工作方式),您需要将_config.yml更改为:

collections:
  popular_posts:
    output: true

这将生成/popular_posts/filename1.html/popular_posts/filename2.html的文件,每个帖子一页。

收藏集是only recently up on GitHub Pages,所以如果你在那里尝试,它就会失败。

如果需要,请查看jekyll-help以获取更多帮助!