Brad Brad - 4 years ago 121
Ruby Question

Creating categories on Jekyll driven site

I'm having a hard time understanding how to generate archive pages for each category I use on my blog. I'd like the user to be able to click on a category and then be taken to a page that lists out all articles with the desired category assigned.

The only way I can think of doing it is by manually creating a specific html file for each category in the root. But I'm sure there must be a more dynamic way?

I have the site hosted on github -

Thanks in advance!


Answer Source

You can generate a list of all the available categories by using the site.categories data, using the first element of each category (which is an array) to get the category name:

{% for cat in site.categories %}
    <li>{{ cat[0] }}</li>
{% endfor %}

And you can generate a list of all the posts in a given category like so:

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

It doesn't seem possible to generate an individual HTML page for each category as you were hoping, but perhaps a good compromise would be to generate a single page containing a list of all categories, where each category contains all the posts in that category. You could then use some simple JavaScript to hide the posts in each category until the category name is selected, giving almost the same user experience as individual archive pages for each category.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download