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?
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.
我相信你想要的是使用capture
构建一个使用类别名称的新变量,如Liquid文档中所述: https : //shopify.github.io/liquid/tags/variable/
{% capture s_c_cat_name%}site.categories.{{cat[0]}}{% endcapture %} ... {% for post in {{s_c_cat_name}} %}
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.