minimal-mistakes 主题中related: false关闭相关文章Related Posts模块的完整指南【免费下载链接】minimal-mistakes:triangular_ruler: Jekyll theme for building a personal site, blog, project documentation, or portfolio.项目地址: https://gitcode.com/gh_mirrors/mi/minimal-mistakes导读本指南以 minimal-mistakes 官方演示站中的docs/_posts/2012-01-02-layout-related-posts-disabled.md一文为切入点系统讲解该 Jekyll 主题相关文章Related Posts模块的开启与关闭机制。文章将结合 single 布局源码 与 page__related 渲染组件 的实际实现说明related: true/false这一 Front Matter 字段如何控制猜您还喜欢You may also enjoy模块的显隐并给出单篇禁用、全局默认、按内容类型区分的三种配置方案以及配套的标题文案与图片定制技巧。读完本文你将能精确控制站点任意页面是否展示相关文章列表并理解其底层渲染逻辑。关联文档速览一个什么都不会出现的测试用例仓库中用于验证该功能的示例文章内容极简其正文只有两句话This post has related posts disabled. Related post links should not appear.对应文档为 docs/_posts/2012-01-02-layout-related-posts-disabled.md--- title: Layout: Related Posts Disabled related: false categories: - Layout - Uncategorized tags: - related posts - layout --- This post has related posts disabled. Related post links should not appear.核心只有一处 YAML Front Matter 字段related: false。它是一篇典型的布局开关验证页——与仓库中成对的姊妹篇 2012-01-02-layout-related-posts.mdrelated: true互为对照用于在演示站点上直观检验该开关的前后差异Front Matter 字段正文表现related: true文章末尾渲染相关文章网格最多 4 篇related: false本文示例文章末尾不渲染任何相关文章区块字段含义与生效范围related是 minimal-mistakes 中控制相关文章模块是否渲染的布尔开关定义于每篇文章的 YAML Front Matter 中。该模块在正文结束后、页脚信息page__meta与翻页导航post_pagination之间插入一个标题为You may also enjoy默认英文文案的文章网格。需要特别说明的是这一开关仅在文章post页面生效。从 single.html 渲染逻辑 可以看出相关文章模块被包裹在{% if page.id and page.related ... %}条件中page.id保证了只有具备文章 ID 的内容即来自_posts或集合的内容才会进入判断普通页面Page即便设置related: true也不会渲染该模块。源码级解析single布局如何响应开关条件分支与降级策略相关文章模块的最终渲染决策位于 single.html{% comment %}!-- only show related on a post page when related: true --{% endcomment %} {% if page.id and page.related and site.related_posts.size 0 %} {% include page__related.html localelocale postssite.related_posts %} {% comment %}!-- otherwise show recent posts if no related when related: true --{% endcomment %} {% elsif page.id and page.related %} {% include page__related.html localelocale postssite.posts %} {% endif %}这里包含一个关键的分支语义分支一related: true且site.related_posts非空即 Jekyll 的lsi相似度计算或相关文章数据可用时渲染由算法推荐的相关文章分支二降级策略related: true但site.related_posts为空时例如在 GitHub Pages 上因lsi被禁用而无法计算相似度自动降级为展示最近发布的文章site.posts保证模块不会出现空白related: false本文示例两个分支均不命中整个page__related区块完全不输出——这正是Related post links should not appear的底层原因。从源码结构可以推断该模块是一个纯渲染层的锦上添花组件不依赖任何插件也不需要前端 JavaScript开关判断完全发生在 Jekyll 构建阶段。渲染组件page__related的细节当开关开启时由 _includes/page__related.html 负责实际渲染{% assign locale include.locale | default: site.locale %} {% assign posts include.posts | where_exp: post, post.hidden ! true %} div classpage__related {% include before-related.html %} h2 classpage__related-title{{ site.data.ui-text[locale].related_label | default: You May Also Enjoy }}/h2 div classgrid__wrapper {% for post in posts limit:4 %} {% if post.id page.id %}{% continue %}{% endif %} {% include archive-single.html localelocale typegrid %} {% endfor %} /div /div几个值得注意的实现细节隐藏文章过滤通过where_exp排除hidden: true的文章草稿与隐藏内容不会混入相关列表自动去重{% if post.id page.id %}{% continue %}{% endif %}保证当前文章绝不会出现在自己的相关列表中对应 changelog 中 Prevent current post from showing in the related posts section 的修复数量上限limit: 4将列表固定为最多 4 篇无论相关文章有多少网格布局复用archive-single.html并以typegrid渲染因此相关文章卡片与归档页的网格卡片样式完全一致。标题文案来自 UI 文本字典区块标题并非硬编码而是从 _data/ui-text.yml 读取当前语言环境的related_label# English (default) en: DEFAULT_EN related_label : You may also enjoy默认值为 You may also enjoydefault兜底为 You May Also Enjoy。该文件内置了 40 余种语言翻译例如中文为猜您还喜欢、日文为関連記事、韩文为참고。若站点配置了locale如zh区块标题会自动切换为对应语言。实战配置单篇禁用、全局默认与按类型区分方案一单篇文章禁用即本文示例的做法在目标文章的 Front Matter 中显式写入related: false即可无需改动_config.yml--- title: 我的某篇文章 related: false ---适用于评论型、公告型等不希望被相关推荐分流注意力的文章。方案二全局默认开启 个别文章覆盖minimal-mistakes 官方推荐的 Front Matter 默认值配置位于 _config.yml 的defaults段详见 docs/_docs/11-posts.mddefaults: # _posts - scope: path: type: posts values: layout: single author_profile: true read_time: true comments: true share: true related: true这样所有_posts下的文章默认展示相关文章若某篇需要关闭只需在该篇 Front Matter 中写related: false覆盖默认值即可。Jekyll 的优先级规则是文章自身 Front Matter 高于defaults这保证了两种配置方式可以无缝组合。方案三按内容类型区分related同样适用于自定义集合Collection。将defaults的scope.type改为集合名称即可为集合内容单独设定默认值defaults: - scope: path: type: recipes # 对应 _recipes 集合 values: layout: single related: true # 菜谱类文章开启相关推荐 - scope: path: type: portfolio # 对应 _portfolio 集合 values: related: false # 作品集条目不推荐相关文章仓库中的_recipes、_portfolio等集合见 docs/_recipes、docs/_portfolio均可按此思路独立控制。验证方式与配套定制本地构建验证由于开关在构建期完成判断验证方式非常直接本地执行bundle exec jekyll serve依赖项见 Gemfile打开关闭了相关文章的页面如本示例文章检查正文结束后没有You may also enjoy 区块再打开related: true的对照文章 2012-01-02-layout-related-posts.md应能看到 1–4 篇网格卡片在_config.yml临时将站点lsi置为falseGitHub Pages 默认禁用可验证降级为最近文章的分支是否正常。定制区块标题不想使用默认文案时可在 _data/ui-text.yml 中覆盖对应语言环境的related_label例如en: related_label: More reading zh: related_label: 延伸阅读在区块前后插入自定义内容page__related.html中通过{% include before-related.html %}预留了扩展点仓库中的 _includes/before-related.html 目前为空文件主题使用者可在此处插入自定义横幅、广告位或说明文字而无需修改主题核心文件对应 changelog 中 Addafter-content.htmlandbefore-related.htmlincludes 的扩展设计。相关文章缩略图teaser提示开启相关文章后网格卡片会优先展示文章 Front Matter 中的header.teaser图片见 docs/_docs/05-configuration.md 对相关文章模块 teaser 图的示例说明。为每篇文章配置独特的 teaser可以让相关文章区块在视觉上更丰满若全部未配置卡片将保持纯文本形态这也是文章正文建议补充excerpt摘要的原因之一。小结开关、分支与扩展点一览配置/机制位置效果related: true文章 Front Matter渲染最多 4 篇相关文章lsi不可用时降级为最近文章related: false文章 Front Matter不渲染相关文章区块本文示例全局默认_config.yml→defaults为 posts / 集合批量设定默认开关区块标题_data/ui-text.yml→related_label多语言文案可按 locale 覆盖去重/上限/过滤_includes/page__related.html排除当前文章、隐藏文章固定 4 篇扩展挂载点_includes/before-related.html在区块前插入自定义内容无论是想要干净的无推荐文章页还是希望全站默认开启并精细控制个别页面minimal-mistakes 都通过related这一个布尔字段配合defaults机制提供了足够灵活的解决方案。把握住 single.html 中两个条件分支的语义你就能准确预测任意一篇文章在构建后是否会带出相关文章模块。【免费下载链接】minimal-mistakes:triangular_ruler: Jekyll theme for building a personal site, blog, project documentation, or portfolio.项目地址: https://gitcode.com/gh_mirrors/mi/minimal-mistakes创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考