【问题标题】:List all posts in both categories (Jekyll / Liquid)列出两个类别中的所有帖子(Jekyll / Liquid)
【发布时间】:2018-05-27 03:01:04
【问题描述】:

我需要在 for 循环中列出所有包含这两个类别的帖子。

{% assign cooking_pages = site.categories.cooking & site.categories.pictures %}

显然以上方法行不通,但这基本上是我想要做的。

我知道我能做到:

{% assign cooking_pages = site.categories.cooking | sort:"title"%} {% for post in cooking_pages %} {% if post.categories contains 'pictures' %} Do whatever I want to do {% endif %} {% endfor %}

但是我的函数使用循环索引和取模,所以 if 语句把事情搞砸了。 有没有办法选择两个类别中都存在的帖子?

这是我的实际代码:

{% assign sorted_pages = (site.categories.2018 | sort:"date") | reverse %}
<table>{% for post in sorted_pages %}
  {% assign loopindex = forloop.index | modulo: 3 %}
  {% if loopindex == 1 %}
    <tr><td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td>
  {% elsif loopindex == 0 %}
    <td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td></tr>
  {% else %}
    <td id="entries"><strong><a href="{{ post.url }}">{{ post.title }}</a></strong></td>
  {% endif %}
 {% endfor %}</table>

它在这个页面上给了我很好的表格:https://200wordrpg.github.io/2018entries

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    这会返回一个按两个类别过滤的列表:

    {% assign list = site.categories.cooking | where_exp: "post", "post.categories contains 'pictures'" %}
    

    您甚至可以链接多个 where_exp 过滤器以过滤三个、四个或更多类别。

    【讨论】:

    • @DavidJacquel 我是否正确理解 where_exp 添加到 list 数组,它首先获取烹饪帖子,然后过滤器添加图片帖子?而不是像我认为的那样过滤烹饪柱?如果是这样,那就太酷了。
    • where_exp 仅保留来自site.categories.cooking 的帖子,这些帖子在其categories 数组中包含“图片”类别。所以它过滤,它没有添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 2015-11-09
    • 2015-09-21
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多