问题描述:

I have a Jekyll blog up and running, but I'd like to do something that seems a bit unorthodox.

Essentially, I've got blog posts in /_posts, and a set of static pages in another folder at /projects.

The top of these projects page looks like this:

---

layout: project

title: My cool project

categories:

- Data Journalism

status: active

---

Now, the biggie: Each category (such as Data Journalism in the example above) has a page with a unique URL, where posts belonging to this category are aggregated.

I would like projects belonging to these categories to be aggregated on these same pages, eg the Data Journalism category page would have a list of projects and a list of posts, all of which are part of this category.

I managed to list all the projects on each category page with

<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>

added in _layouts/category_index.html. But it shows all projects, not only the one belonging to the page category. In the example, My cool project should only be listed in the Data Journalism category page.

Can you help? I'd be most grateful!

EDIT: Thanks to Davic Jacquel for the solution. His answer's below, marked as solved, and here is the tweak I had to make:

<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 : reading your plugin code makes me realize that you have a page.category variable in all your categories pages.

Note : current page's data are stored in the page variable. So, when you loop, try not use page as a storage variable to avoid collision.

In _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>
相关阅读:
Top