【问题标题】:Jekyll: Filter posts by subdirectoryJekyll:按子目录过滤帖子
【发布时间】:2016-02-09 03:01:22
【问题描述】:

我正在使用 jekyll 在 github.io 上托管一个网站

主页有一个“最近的帖子”部分,显示最新的 10 个帖子。

我在 index.html 中使用以下代码来生成列表:

<ul class="post-list">
{% for post in site.posts limit:10 %} 
  <li><article><a href="{{ site.url }}{{ post.url }}">{{ post.title }} <span class="entry-date"><time datetime="{{ post.date | date_to_xmlschema }}">{{ post.date | date: "%B %d, %Y" }}</time></span>{% if post.excerpt %} <span class="excerpt">{{ post.excerpt }}</span>{% endif %}</a></article></li>
{% endfor %}
</ul>

_posts 目录有 3 个子目录。上面的代码从所有三个目录生成一个列表。我想在“最近的帖子”目录中排除其中一个目录中的帖子。

更一般地,如何选择 _posts 目录中的子目录子集?

【问题讨论】:

    标签: github jekyll


    【解决方案1】:

    我通过在 for 循环中添加 if 语句解决了这个问题:

    {% if post.path contains 'xxx' or post.path contains 'yyy'%}
      <li><article>...</article></li>
    {% endif %}
    

    这适用于简单的情况,但对于更复杂的情况可能会变得很难看。

    for 循环似乎在遍历一个列表,但我不知道如何使用我想要的元素生成一个列表。我找不到 Jekyll 使用的脚本语言的良好参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-12
      • 2015-11-09
      • 2019-03-26
      • 1970-01-01
      • 2019-02-15
      • 2012-04-14
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多