如果 YAML Frontmatter 变量设置为特定值,只需在 jekyll 博客上显示一些帖子

Just show up some posts on a jekyll blog if a YAML Frontmatter variable is set to a specific value

我目前正在开发一个基于 Jekyll 和 GH-pages 的静态博客。 在我的 post 概览网站的顶部,我确实有一个部分,我想在其中放置一些特色博客posts。

我可以将值 "featured" 添加到那些 post 的 YAML Front Matter 中的 "tags" 并插入行:

{% for post in site.tags.featured %}

尽管如此,我是那些不喜欢坚持想到的第一个解决方案的复杂人之一(尽管它可能是最简单的解决方案)。

我的想法是在我的 YAML Front Matter 中添加一个新变量 featured 并使用值 yesno 进行标记(这里同样如此:是的,我知道truefalse 会更容易,但我希望能够将解决方案转移到另一个问题)如果它是特色内容(并且应该在本节中显示)或者如果不是。

对于 jekyll 专家来说,这可能是一个简单的解决方案,但我对那种静态站点生成器还很陌生,很想听听您的想法。

如果您分配 featured: truefeatured: yes,此过滤器将起作用:

{% assign featuredPosts = site.posts | where:"featured", true %}

A {% for post in featuredPosts %} 就可以了。

注意:所有 Truthy and Falsy in Liquid 在实际的 Jekyll 中都不起作用。