Reputation: 4973
I have the following layout:
---
layout: default
---
{% include header.html %}
<ul>
{% for cat in site.categories %}
<li>
{% assign cat_name = cat[0] %}
<div class="Projects">
<h1>{{ cat[0] }}</h1>
<h2>{{ cat_name }}</h2>
<ul>
{% for post in site.categories.cat_name %}
<li>
<span class="date">{{ post.date | date: '%Y %b %d' }}</span> - <a href="{{ post.url }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
</div>
</li>
{% endfor %}
</ul>
This attempts to look through my site's categories and then for each one, assign the category name as "cat_name" and then for each of those, it uses {% for post in site.categories.cat_name %}
to loop through the posts in that category.
This doesn't work. The line:
<h2>{{ cat_name }}</h2>
Does work. It shows "opinion" for instance which is one of the categories, so I know the assignment worked. And {% for post in site.categories.opinion %}
works for instance. So its just something about passing that variable in there that's not working. How do I do this?
Upvotes: 2
Views: 1124
Reputation: 141
I believe what you want is to use capture
to build a new variable that uses the category name, as documented in the Liquid docs here: https://shopify.github.io/liquid/tags/variable/
{% capture s_c_cat_name%}site.categories.{{cat[0]}}{% endcapture %} ... {% for post in {{s_c_cat_name}} %}
Upvotes: 1
Reputation: 4973
I should have done {% for post in site.categories[cat_name] %}
instead of {% for post in site.categories.cat_name %}
That made it work for me.
Upvotes: 3