简体   繁体   中英

Category Page in Jekyll

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM