Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jekyll

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.

like image 871
tamouse Avatar asked Sep 21 '13 10:09

tamouse


1 Answers

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 %}
like image 180
ljs.dev Avatar answered Nov 09 '22 04:11

ljs.dev