列出相关类别页面中的非帖子页面

List non-posts pages in relevant category pages

我有一个 Jekyll 博客 运行,但我想做一些看起来有点非正统的事情。 基本上,我在 /_posts 中有博客文章,在 /projects 的另一个文件夹中有一组静态页面。

这些项目页面的顶部如下所示:

---
layout: project
title: My cool project
categories:
- Data Journalism
status: active
---

现在,重要的是:每个类别(如上例中的 Data Journalism)都有一个具有唯一 URL 的页面,其中属于该类别的帖子被聚合。

我希望将属于这些类别的项目汇总到这些相同的页面上, 例如 Data Journalism 类别页面将有一个项目列表和帖子列表,所有这些都属于此类别。

我设法在每个类别页面上列出了所有项目

<ul class="posts">
  {% for page in site.pages %}
    {% if page.layout == 'project' %}
      {% if page.status == 'active' %}
        <h2><a href="{{ page.url }}">{{ page.title }}</a></h2>
        <div> {{ page.description }}</div>
      {% endif %}
    {% endif %}
  {% endfor %}
</ul>

已添加到 _layouts/category_index.html。但是它显示了 all 个项目,而不仅仅是属于页面类别的项目。在示例中,My cool project 应该只在 Data Journalism 类别页面中列出。

你能帮忙吗?将不胜感激!

编辑: 感谢 Davic Jacquel 提供的解决方案。他的回答在下面,标记为已解决,这是我必须做的调整:

<ul class="posts">
  {% for p in site.pages %}
    {% if p.layout == 'project' and p.status == 'active' %}
    {% assign cat = p.categories | downcase %}
      {% if cat contains page.category %}
        <h2><a href="{{ site.baseurl }}{{ p.url }}">{{ p.title }}</a></h2>
        <div> {{ p.description }}</div>
      {% endif %}
    {% endif %}
  {% endfor %}
</ul>

Edit :阅读您的插件代码让我意识到您在所有类别页面中都有一个 page.category 变量。

注意:当前页面的数据存储在page变量中。所以,循环的时候,尽量不要用page作为存储变量,避免冲突。

_layouts/category_index.html中:

---
layout: default
---
<ul class="posts">
  {% for p in site.pages %}
    {% if p.layout == 'project' and p.status == 'active' %}
      {% if p.categories contains page.category %}
        <h2><a href="{{ site.baseurl }}{{ p.url }}">{{ p.title }}</a></h2>
        <div> {{ p.description }}</div>
      {% endif %}
    {% endif %}
  {% endfor %}
</ul>