You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Jekyll的Blog页面实现二级分组展示指定文章?

解决方案:在博客页面分两组展示特定文章

当然可以实现这个需求!下面分享两种实用的方法,帮你轻松给博客文章分组展示:

方法一:通过Front Matter分类字段(推荐,灵活度高)

这种方法需要给每篇博客文章添加分类标记,然后在blog.md里用Liquid语法筛选展示:

步骤1:给_blog中的文章添加分类字段

打开_blog文件夹里的每篇.md文件,在顶部的Front Matter中加入category字段,比如:

---
title: "我的第一篇技术博客"
date: 2024-01-01
category: "技术笔记"
---

文章内容...

另一组的文章就设置category: "生活感悟",依此类推。

步骤2:修改blog.md页面的代码

把原来的blog.md内容替换成下面的代码,它会分别筛选并展示两组文章:

---
permalink: /blog/
layout: page
classes: wide
---

## 技术笔记
{% assign tech_posts = site.blog | where: "category", "技术笔记" %}
{% for post in tech_posts %}
- **{{ post.title }}** - {{ post.date | date: "%Y-%m-%d" }}
  > {{ post.excerpt | strip_html | truncatewords: 20 }}
  > [阅读全文]({{ post.url }})
{% endfor %}

---

## 生活感悟
{% assign life_posts = site.blog | where: "category", "生活感悟" %}
{% for post in life_posts %}
- **{{ post.title }}** - {{ post.date | date: "%Y-%m-%d" }}
  > {{ post.excerpt | strip_html | truncatewords: 20 }}
  > [阅读全文]({{ post.url }})
{% endfor %}

这里用site.blog调用整个博客集合,where过滤器精准筛选对应分类的文章,还添加了摘要和日期,让页面信息更丰富。

方法二:通过子文件夹分组(适合文件结构清晰的场景)

如果你希望直接通过文件位置来区分分组,不用手动加Front Matter,可以试试这种方法:

步骤1:在_blog下创建子文件夹

在_blog文件夹里新建两个子文件夹,比如tech-notes和life-stories,然后把对应文章放到各自的文件夹中。

步骤2:修改blog.md页面的代码

用where_exp过滤器根据文章路径筛选:

---
permalink: /blog/
layout: page
classes: wide
---

## 技术笔记
{% assign tech_posts = site.blog | where_exp: "post", "post.path contains '_blog/tech-notes'" %}
{% for post in tech_posts %}
- [{{ post.title }}]({{ post.url }}) - {{ post.date | date: "%Y-%m-%d" }}
{% endfor %}

---

## 生活感悟
{% assign life_posts = site.blog | where_exp: "post", "post.path contains '_blog/life-stories'" %}
{% for post in life_posts %}
- [{{ post.title }}]({{ post.url }}) - {{ post.date | date: "%Y-%m-%d" }}
{% endfor %}

这种方法的好处是不用修改每篇文章的Front Matter,只要维护文件结构就行。

小提示

  • 如果你需要更多分组,只要复制对应的筛选和循环代码块,修改分类名称或路径即可。
  • 如果想保留网格布局,可以把循环里的列表改成网格样式的HTML结构(比如用自定义CSS实现卡片式布局)。

内容的提问来源于stack exchange,提问作者Dillon Barker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 09:43:13