Skip to content

分类操作指南

Actions

Action用法说明
list-categories/halo list-categories [--limit=N] [--page=N] [--sort=xxx]列出分类(--limit=0 --page=0 获取全部)
create-category/halo create-category --display-name=名称 [--slug=xxx]创建分类
get-category/halo get-category <name>获取分类详情
update-category/halo update-category <name> [--display-name=xxx] [--priority=N]更新分类
delete-category/halo delete-category <name>删除分类

参数说明

参数说明适用操作
--display-name=分类显示名(必填)create-category/update-category
--slug=分类别名create-category/update-category
--cover=封面 URLcreate-category/update-category
--description=分类描述create-category/update-category
--priority=N排序优先级,默认 0create-category/update-category
--hide-from-list隐藏分类不在列表显示update-category
--limit=N每页数量,默认 20list-categories
--page=N页码,从 1 开始list-categories
--sort=排序字段,如 spec.priority,asclist-categories

⚠️ 重要说明

  1. 全部走 Extension API — 分类的 list/create/get/update/delete 均使用 Extension API (/apis/content.halo.run/v1alpha1/categories)。
  2. 树形结构 — 分类支持层级关系,spec.children 数组存储子分类的 metadata.name。
  3. 乐观锁 — 更新需要 metadata.version,脚本自动获取最新版本并在 409 冲突时重试。
  4. metadata.name 自动生成create-category 自动生成 {slug}-{timestamp} 格式的 name。
  5. 全量获取list-categories --page=0 --limit=0 可获取所有分类(不分页)。
  6. 无变更检测update-category 在没有实际变更时直接返回"无变更",不发起 PUT 请求。
  7. hideFromList 限制 — 仅在一级分类(根节点)上生效,隐藏后该分类及其子分类和关联文章不在分类列表显示,但仍可通过永久链接访问。

API 参考

Halo Categories API 参考

所有调用走 Node.js script,本文档仅在排查报错时参考

API 端点

方法路径说明
GET/apis/content.halo.run/v1alpha1/categories列出分类
POST/apis/content.halo.run/v1alpha1/categories创建分类
GET/apis/content.halo.run/v1alpha1/categories/{name}获取分类
PUT/apis/content.halo.run/v1alpha1/categories/{name}更新分类
DELETE/apis/content.halo.run/v1alpha1/categories/{name}删除分类

创建分类请求体

json
{
  "apiVersion": "content.halo.run/v1alpha1",
  "kind": "Category",
  "metadata": {
    "name": "category-slug-20240101120000"
  },
  "spec": {
    "displayName": "分类显示名",
    "slug": "category-slug",
    "priority": 0,
    "children": [],
    "cover": "",
    "description": ""
  }
}
  • metadata.name 自动生成,格式为 {slug}-{timestamp}
  • spec.displayNamespec.slug 为必填
  • spec.priority 排序优先级,默认 0
  • spec.children 子分类的 metadata.name 数组

更新分类

  • 路径参数: name — 分类的 metadata.name
  • 使用 GET-modify-PUT 模式,先获取完整分类对象,修改 spec 字段后 PUT 回去
  • 更新需要 metadata.version(乐观锁),脚本会自动重试 409 冲突
  • 可更新字段: displayName, slug, cover, description, priority, hideFromList

删除分类

  • 路径参数: name — 分类的 metadata.name
  • 删除前脚本会先 GET 确认分类存在,不存在则报 404 错误

查询参数

参数类型说明
pageinteger页码,从 1 开始,0 表示不分页
sizeinteger每页数量,0 表示不分页
labelSelectorstring[]标签选择器,如 hidden!=true
fieldSelectorstring[]字段选择器,如 metadata.name==halo
sortstring[]排序条件,格式 `property,(asc

数据结构

Category 对象

json
{
  "apiVersion": "content.halo.run/v1alpha1",
  "kind": "Category",
  "metadata": {
    "name": "category-name",
    "version": 1,
    "creationTimestamp": "2024-01-01T00:00:00Z",
    "labels": {},
    "annotations": {}
  },
  "spec": {
    "displayName": "分类显示名",
    "slug": "category-slug",
    "priority": 0,
    "children": ["child-category-name"],
    "cover": "",
    "description": "",
    "hideFromList": false,
    "template": "",
    "postTemplate": "",
    "preventParentPostCascadeQuery": false
  },
  "status": {
    "permalink": "https://example.com/categories/category-slug",
    "postCount": 10,
    "visiblePostCount": 8
  }
}

CategorySpec 字段

字段类型必填说明
displayNamestring分类显示名称(最少 1 字符)
slugstring分类别名(最少 1 字符)
priorityinteger排序优先级,默认 0
childrenstring[]子分类的 metadata.name 数组
coverstring封面图片 URL
descriptionstring分类描述
hideFromListboolean是否在分类列表中隐藏(仅一级分类生效)
templatestring分类页面模板
postTemplatestring分类下文章页面模板(优先级低于文章自身模板)
preventParentPostCascadeQueryboolean是否阻止父分类级联查询本分类下的文章

CategoryStatus 字段

字段类型说明
permalinkstring分类永久链接
postCountinteger包括当前和其下所有层级的文章数量 (depth=max)
visiblePostCountinteger包括当前和其下所有层级的已发布且公开的文章数量

基于非商业使用许可证发布