Jekyll2.0列出了分类中断的所有帖子
我最近从Jekyll 1.0之前的版本升级到了2.0 在我的原始代码中,在每个博客帖子上,它都会列出与当前正在查看的帖子属于同一类别的所有帖子的标题。在此之前,此代码有效:Jekyll2.0列出了分类中断的所有帖子,jekyll,liquid,Jekyll,Liquid,我最近从Jekyll 1.0之前的版本升级到了2.0 在我的原始代码中,在每个博客帖子上,它都会列出与当前正在查看的帖子属于同一类别的所有帖子的标题。在此之前,此代码有效: {% for post in site.categories.[page.category] %} <li {% if page.title == post.title %} class="active" {% endif %}> <a href="{{ post.url}}">{{
{% for post in site.categories.[page.category] %}
<li {% if page.title == post.title %} class="active" {% endif %}>
<a href="{{ post.url}}">{{ post.title }}</a></li>
{% endfor %}
为什么我不能像以前那样动态检查类别?是否有一种解决方法,而不是使用
if
语句 我想出来了。在每一篇文章中,我都有我的YAML front matter类别变量,用大写或驼峰字母表示。示例:category:ABC
或category:Zyx
执行page.category
操作将始终返回前面事项中所写的实际类别,即ABC
或Zyx
。但是,site.categories.[CAT]
只接受小写形式的CAT
(液体语言中的小写形式)
因此,这将适用于site.categories.['abc']
或site.categories.['xyz']
。
但这将失败site.categories.['ABC']
或site.categories.['Xyz']
。这与执行site.categories.[page.categories]
相同
解决方案。以小写形式指定当前页面类别,如下所示:
{% assign cat = page.category | downcase %}
{% for post in site.categories.[cat] %}
<li {% if page.title == post.title %} class="active" {% endif %}>
<a href="{{ post.url}}">{{ post.title }}</a></li>
{% endfor %}
{%assign cat=page.category | downcase%}
{%用于站点中的帖子。类别。[cat]}
{%endfor%}
{% assign cat = page.category | downcase %}
{% for post in site.categories.[cat] %}
<li {% if page.title == post.title %} class="active" {% endif %}>
<a href="{{ post.url}}">{{ post.title }}</a></li>
{% endfor %}