简体   繁体   中英

Jekyll, show category for a single post

Using Jekyll - I want to display a list of categories to which a given post belongs.

In my post.html layout, I have the following block of code which renders ALL of the site's categories.

 <p>Posted in: {% for category in site.categories %} <a href="/blog/{{ category | first | slugize }}/"> {{ category | first }} </a> {% endfor %} </p> 

What I would like to show instead is a list of the categories that this single post is in. I've tried replacing site.categories with page.categories & post.categories, to no avail.

Any help would be appreciated.

Instead of using site.categories , use page.categories . Also, you do not need the first -filter since you already loop over the array:

{% for category in page.categories %}
  <a href="/blog/{{ category | slugize }}/">
    {{ category }}
  </a>
{% endfor %}

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