Hexo 博客创建 categories 和 tags 页面

使用 Hexo 写文章时,可以使用 categories:tags: 来指定文章所属的分类和标签,如:

1
2
3
4
5
6
---
title: play-json 中使用默认值
date: 2017-4-10 08:14:40
tags: play-json
categories: Scala
---

使用上面的元数据,Hexo 在渲染时会自动创建 Scala 分类和 play-json 标签(如果分类和标签不存在),我们可以通过 https://domain.com/categories/Scala/https://domain.com/tags/play-json/ 来查看对应分类和标签的所有文章。但 Hexo 并不会为我们自动创建 所有分类所有标签 的页面,需要我们自己来创建。

使用下面的命令创建两个名为 categoriestags 的页面:

1
2
hexo new page categories
hexo new page tags

上述命令会在 blog_dir/source 中创建两个名为 categoriestags 的目录,每个目录中都有一个 index.md 文件。我们只需要修改这两个 index.md 文件就能达到需要的效果。
分别编辑 categories/index/mdtags/index.md 文件,将内容替换为:

1
2
3
4
5
---
title: 分类
date: 2018-10-10 12:22:12
type: "categories"
---

1
2
3
4
5
---
title: 标签
date: 2018-10-10 12:21:48
type: "tags"
---

其中 title 字段可以根据自己需要定制,而 type 字段必须和上面保持一致。如果启用了相关的评论功能(如 Disqus 等),还需要添加一行 comments: false 来防止渲染的页面中有评论模块。

此时,就可以配置主题(以 Next 为例)的菜单属性了:

1
2
3
4
5
6
7
8
9
menu:
home: / || home
#about: /about/ || user
tags: /tags/ || tags
categories: /categories/ || th
archives: /archives/ || archive
#schedule: /schedule/ || calendar
#sitemap: /sitemap.xml || sitemap
#commonweal: /404/ || heartbeat

使用 hexo g 重新生成站点来看看效果吧~