且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

GitHub Pages中的分层类别

更新时间:2023-01-30 13:18:44

page.categories是一个列表

page.categories is a list

https://***.com/a/23927986

{% for cat in page.categories %}
  <h1>{{ cat }}</h1>
  <ul>
    {% for post in site.categories[cat] %}
      <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    {% endfor %}
  </ul>
{% endfor %}

摘自jekyll关于page.category的文档 http://jekyllrb.com/docs/variables /#page-variables

From jekyll's documentation about page.category http://jekyllrb.com/docs/variables/#page-variables

此帖子所属类别的列表.类别是 从_posts目录上方的目录结构派生而来.为了 例如,/work/code/_posts/2008-12-24-closures.md上的帖子将具有 此字段设置为['工作','代码'] .这些也可以在 YAML重要事项.

The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter.

您应该能够轻松地向每个文件夹添加一个简单的动态index.html,并且类别应自动分层.

You should be easily able to add a simple dynamic index.html to every folder and the categories should be hierarchical automatically.

以上操作无效.您需要将每个层次结构的类别组合视为唯一项.合并索引页面的类别,并将其与网站中的所有帖子进行比较.

The above does NOT work. You need to treat the combination of categories of each hierarchy as a unique item. Concat the index page's categories, and compare that against all the posts in the site.

/foo/bar/_posts/2016-08-01-foo-bar-test.html

/foo/bar/_posts/2016-08-01-foo-bar-test.html

---
categories:
  - foo
  - bar
title: test foo bar
---

<h2>Foo Bar</h2>

/var/bar/_posts/2016-08-01-test-var-bar.html

/var/bar/_posts/2016-08-01-test-var-bar.html

---
categories:
  - var
  - bar
title: test var bar
---

<h2>Var Bar</h2>

/foo/bar/index.html

/foo/bar/index.html

---
categories:
 - foo
 - bar
---

{% assign pagecat = page.categories | join ' ' | append: ' '%}
{% assign pagecatlen = page.categories.size %}
  <h1>{{ cat }}</h1>
  <ul>
    {% for post in site.posts %}
    {% assign postcat = '' %}
    {% for thispostcat in post.categories limit: pagecatlen %}
      {% assign postcat = postcat | append: thispostcat %}
      {% assign postcat = postcat | append: ' ' %}
    {% endfor %}

    {% if (postcat == pagecat) %}
      <li><a href="{{ post.url }}">{{ post.title }}</a></li>
    {% endif %}
    {% endfor %}
  </ul>

对于_posts中的文件,类别是可选的,但是对于每个索引文件的首页而言,类别都是必需的.

The categories are optional for the files in _posts, but they are required for the front matter of each index file.