Category Page in Jekyll

拈花ヽ惹草 提交于 2019-12-12 04:58:37

问题


I have a category.html page that will loop through a certain category of posts.

{% for post in site.categories.tech %}

What I want to display is from the index.html page, when the user clicks

{{ post.categories }}

Which is generated from a loop that displays all of the posts.

{% for post in site.posts %}

I need that {{ post.categories }} variable to replace the .tech in the categories.html like this

{% for category in site.categories.{{ post.categories }} %}

I don't know how to pass the variable from index.html to categories.html


回答1:


Site categories is an object where each key is the category name. So

{{ site.categories }}

Outputs something like this:

{
  "Category name" => [#, #, #, #, #, #, #],
  "Another category name" => [#, #, #, #, #, #, #],
  ...
}

Therefore, square brackets should do it. So if in the front matter of your index.html you have

---
categories: tech
---

The code below will render all your posts under tech category.

{% for post in site.categories[post.categories] %}
  {{ post }}
{% endfor %}

Of course this won't work if you have multiple categories in the front matter, but I don't know your use case.



来源:https://stackoverflow.com/questions/44962310/category-page-in-jekyll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!