简体   繁体   中英

In Jekyll: Want to use category (or tag) name in a list, but not all posts in a category

I want to create a category index that simply creates a list of the categories in the site and a link to each category's index, as generated by the category index generator.

---
layout: default
title: "Categories"
---

<div id="category-index" class="container">
  <ul id="categories">
    {% for category in site.categories %}
    <li><a href="/{{category}}/">{{ category }}</a></li>
    {% endfor %}
  </ul>
</div>

but this give the entire array of posts. I just want the category name. I've tried category.key , category.name , and category.title , but I just do not seem to be able to get just that name.

I am also hoping to do the same with site.tags, assuming there is a similar answer.

You are looking at the correct site.categories , but for the name, you must reference the first object in the array as category[0] .

Regarding tag lists, you may want to look at this helper class .

UPDATE:

A more elegant way to code this, using the Liquid templating engine's filter, would be:

{% for category in site.categories %}
    {{ category | first }}
{% 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