如何为每个类别设置不同的布局?

How to have different layouts per category?

如何为 post 个类别设置不同的布局?

例如,如果我将 "toys" 和 "swords" 作为我的 post 的类别,我希望页面列表 "toys" 具有不同的布局比页面列表 "swords"。如果 post 所在的类别没有自己的布局,那么使用默认的 "products" 布局会更好。关于如何使用 jekyll 实现这个的任何想法?

由于您的文档的语义不会在类别之间改变,我认为您必须使用 CSS 来解决这个问题。

sword_list.html

---
category: sword
layout: product_listing
title: swords list
---
{% include product_listing_loop %}

_includes/product_listing_loop.html

<div class="{{page.category}}">
<ul>
{% for post in categories[page.category] %}
  <li>{{ post.title }}</li>
{% endfor %}
</ul>
</div>

您甚至可以在主模板的 body 元素上使用 page.category 作为 class。

编辑:如果您确实需要不同的标记,您可以尝试:

sword_list.html

---
category: sword
layout: product_listing
title: swords list
---
{% assign include_name = 'category_' | append: {{page.category}} %} 
{% include {{include_name}} %}

这将调用 _includes/category_sword.html