首页 > 解决方案 > 如何创建和显示自定义 Jekyll 元数据

问题描述

我有一个 Jekyll 私人博客(即实验室笔记本),它使用了极好的最小错误主题。我为我的每篇博客文章大量使用标签,并且可以看到标签列表。

我也喜欢跟踪我在博客文章中提到的人,所以我为每篇文章添加了额外的元数据,如下所示:

---
title: "My great experiment"
tags:
  - physics
  - chemistry
  - Higgs boson
people:
  - Albert Einstein
  - Galileo Galilei
  - Marie Curie
---

我想做的是查看我博客中提到的所有人——在所有博客文章中——但 Jekyll 似乎没有填充site.people变量。我试图site.people像这样可视化变量:

{
{
"people": [
    {%- for person in site.people -%}
    "{{ person[0] }}"{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
],

}
}

但我在呈现的网页上看到的只是:

{ { “people”: [],

} }

我需要做什么才能让 Jekyll 看到人员元数据?我需要以某种方式告诉 Jekyll 还有其他需要查看的元数据吗?

标签: frontendjekyllmetadataliquid

解决方案


我认为您需要在第二个代码片段中替换sitepage,请参阅变量处理。

  • 站点:全球网站(例如_config.yml)
  • 页面:当前页面

另外我删除了数组索引[0]

{
{
"people": [
    {%- for person in page.people -%}
    "{{ person }}"{% unless forloop.last %},{% endunless %}
    {%- endfor -%}
],

}
}

我的输出/结果


{ { “people”: [“Albert Einstein”,”Galileo Galilei”,”Marie Curie”],

} }

2020 年 10 月 23 日更新:

_config.yml声明所有物理学家的全局片段。

...

physicists:
  - Albert Einstein
  - Galileo Galilei
  - Marie Curie
  - Isaac Newton
  - Nikola Tesla
  - Max Planck

...

获取任何帖子上所有物理学家的列表

---
title: "My great experiment"
tags:
  - physics
  - chemistry
  - Higgs boson
people:
  - Albert Einstein
  - Galileo Galilei
  - Marie Curie
---

## List all Physicists

{% for itm in site.physicists %}
    {{ itm }}
{% endfor %}

推荐阅读