Jekyll 杰基尔按类别显示帖子
我对此感到困惑-非常感谢您的帮助 我想按类别显示我所有的Jekyll帖子列表。我知道第三行不正确,但我不知道它应该是什么。有什么想法吗?谢谢Jekyll 杰基尔按类别显示帖子,jekyll,liquid,Jekyll,Liquid,我对此感到困惑-非常感谢您的帮助 我想按类别显示我所有的Jekyll帖子列表。我知道第三行不正确,但我不知道它应该是什么。有什么想法吗?谢谢 {% for category in site.categories %} <h3>{{ category | first }}</h3> {% for post in page.categories.category %} {{ post.title }}<br> {% endfor
{% for category in site.categories %}
<h3>{{ category | first }}</h3>
{% for post in page.categories.category %}
{{ post.title }}<br>
{% endfor %}
{% endfor %}
{%用于site.categories%}
{{类别|第一}}
{page.categories.categority%%中的文章的百分比}
{{post.title}}
{%endfor%}
{%endfor%}
明白了!在列出单个帖子之前需要一个中间帖子循环
<ul>
{% for category in site.categories %}
<li><a name="{{ category | first }}">{{ category | first }}</a>
<ul>
{% for post in category.last %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
{site.categories%中的类别为%s}
{%endfor%}
{%endfor%}
仅供参考,如果有人只想在一个类别中列出帖子,这是可行的(与上面的示例不同,因为类别返回一个帖子列表
<p>Posts in category "basic" are:</p>
<ul>
{% for post in site.categories.basic %}
{% if post.url %}
<li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
“基本”类中的帖子包括:
-
{site.categories.basic%}
{%if post.url%}
{%endif%}
{%endfor%}
我一时想不起确切的语法,但类似于下面的代码应该检索类别名称,以允许您检索每个类别的帖子
{% for category in site.categories %}
{% assign cat_name = category[0] %}
{% for post in site.categories.cat_name %}
...
{% endfor%}
{% endfor %}
下面是一个使用排序的答案(很有用!):
{%comment%}
#
#通过添加“|反向”更改日期顺序
#要按标题或其他变量排序,请使用{%assign sorted_posts=category[1]| sort:'title%}
#
{%endcomment%}
{%assign sorted_cats=site.categories | sort%}
{已排序的类别的%u%}
{%assign sorted_posts=category[1]| reversed%}
{{类别[0]|大写}
{已排序的_posts%}
{%endfor%}
{%endfor%}
这不是我的,它取自。现在有一个官方插件可供使用 为了利用它, 将
jekyll归档文件
添加到Gemfile
和\u config.yml
文件中
根据需要添加类似于以下内容的配置
jekyll-archives:
enabled: all
layouts:
year: archive/year
month: archive/month
day: archive/day
tag: archive/tag
category: archive/category
permalinks:
year: '/:year/'
month: '/:year/:month/'
day: '/:year/:month/:day/'
tag: '/tags/:name/'
category: '/category/:name/'
根据存档类型,布局可以使用以下页面属性
- 页面。键入-(以下任意一项。
年
,月
,日
,标签
,类别
)
- page.title-(仅适用于类型标签和类别。
Nil
否则。)
- page.date-(根据
page.type
您应该解析出日期和月份字段)
- page.posts-(此存档的帖子列表)
这是一个基于年份的存档布局示例
<h1>Archive of posts from {{ page.date | date: "%Y" }}</h1>
<ul class="posts">
{% for post in page.posts %}
<li>
<span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
来自{{page.date | date:%Y}的帖子存档
{page.posts%%中的post为%s}
-
{post.date | date:“%b%-d,%Y”}
{%endfor%}
类别
{site.categories%中的类别为%s}
{%assign cat=category[0]}
{%用于站点中的帖子。类别[cat]}
{{post.date}
{%endfor%}
{%endfor%}
我在本地进行了测试,发现第一个{posts}是类别名称,在html中是一个空行,因此我在
前面添加了{%if post.url%}以删除类别行嘿,谢谢,它工作得很好!不过我有一个问题(对不起,total jekyll noob),可能有助于改进你的答案:你知道吗(如果可能)如何将此代码放入位于/category/的页面中,从url读取并相应地提取帖子列表?(编辑)我的问题在这里重复:这是一个更好的答案,避免了被接受的答案所必需的建议黑客攻击。对于包含空白的类别名称,如“foo bar”,我如何才能做到这一点?不起作用。阅读{%for post in site.categories[cat_name]}
<h1>Archive of posts from {{ page.date | date: "%Y" }}</h1>
<ul class="posts">
{% for post in page.posts %}
<li>
<span class="post-date">{{ post.date | date: "%b %-d, %Y" }}</span>
<a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>
<h5>Categories</h5>
{% for category in site.categories %}
{% assign cat = category[0] %}
<h6><a href="#">{{ cat }}</a></h6>
{% for post in site.categories[cat] %}
<a href="{{ post.url }}">{{ post.title }}</a> <small>{{ post.date }}</small>
{% endfor %}
{% endfor %}