2

In Jekyll, a post can have multiple categories. For example, a recipe for spaghetti might have the categories dinner and food. Is it possible--without plugins--to iterate over the other categories (different dinner times) of a category (food)? For example, I want to generate the following page for the category food:

Dinner:
* Spaguetti
* Meatloaf

Breakfast
* Cheerios
* Oatmeal

Lunch
* BLT
schmmd
  • 18,650
  • 16
  • 58
  • 102
  • schmmd, did you find a solution? Post back here so others can see an answer to your question. Maybe the latest Jekyll has made this easier. – Daniel Baird Aug 06 '14 at 07:00

1 Answers1

0

Like a lot of Jekyll work, the answer is to loop through stuff way more than you think you should :)

Just loop through all categories or tags or whatever for your entire site, and use an if tag to avoid outputting ones that this post doesn't have.

Then inside the loop body, loop through every post on your site, using if tags again to avoid outputting the ones that don't have the category.

Here's some code that will do it for tags, I think if you replace tags with categories it'll work the same. I've lightly modified it from my own site, sorry if there's a typo or two:

{% for topic in site.tags | sort_by:topic order:ascending %}
    {% if topic == whatever_topic_you_have %}
        <section class="topic">
            <h1><a name="{{ topic }}">{{ topic }}</a></h1>
            {% for item in site.posts %}
                {% if item.tags contains topic %}
                    ...show your post/item here...
                {% endif %}
            {% endfor %}
        </section>
    {% endif %}
{% endfor %}
Daniel Baird
  • 2,239
  • 1
  • 18
  • 24
  • I tried adopting this approach for multiple subcategories, but it seems catered to one category at a time. I've asked a follow-up question [here](http://stackoverflow.com/questions/39237526/list-subcategories-in-githubpages) if you feel like checking it out. – Kevin Workman Aug 30 '16 at 21:43