Jekyll按类别显示帖子

Ale*_*x G 43 liquid jekyll

在这个问题上摸不着头脑 - 非常感谢.

我想显示按类别组织的所有Jekyll帖子的列表.我知道第3行不正确,但我无法弄清楚应该是什么.有任何想法吗?谢谢!

{% for category in site.categories %}
    <h3>{{ category | first }}</h3>
    {% for post in page.categories.category %}
      {{ post.title }}<br>
    {% endfor %}            
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

Ale*_*x G 66

得到它了!在列出个别帖子之前需要一个中间帖子循环

<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>
Run Code Online (Sandbox Code Playgroud)

  • 我在我的本地测试,发现{{posts}}中的第一个是类别名称,并且在html中将是一个空行,所以我在{li> <a href ="之前添加{%if post.url%} {{post.url}}"> {{post.title}} </a> </ li>`删除类别行 (24认同)

tim*_*tim 20

fyi,如果有人想只列出一个类别的帖子,这是有效的(与上面的例子不同,因为该类别返回一个帖子列表...

<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>
Run Code Online (Sandbox Code Playgroud)


Raj*_*gan 7

现在有一个官方插件可用于此。jekyll-archives

为了利用它,

添加jekyll-archives到您的Gemfile_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/'
Run Code Online (Sandbox Code Playgroud)

可以layouts根据archive type.

  • page.type - (以下任意一项。year,,,,,, )monthdaytagcategory
  • 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>
Run Code Online (Sandbox Code Playgroud)