RSS 模板
配置
默认情况下,构建站点时,Hugo 会为首页、栏目、分类法和术语页面生成 RSS 订阅源。您可以通过站点配置来控制订阅源的生成。例如,要为首页和栏目页面生成订阅源,而不要为分类法和术语页面生成订阅源:
hugo.
 
 
 
outputs:
  home:
  - html
  - rss
  section:
  - html
  - rss
  taxonomy:
  - html
  term:
  - html
[outputs]
  home = ['html', 'rss']
  section = ['html', 'rss']
  taxonomy = ['html']
  term = ['html']
{
   "outputs": {
      "home": [
         "html",
         "rss"
      ],
      "section": [
         "html",
         "rss"
      ],
      "taxonomy": [
         "html"
      ],
      "term": [
         "html"
      ]
   }
}
要禁用所有[页面类型]的订阅源生成:
hugo.
 
 
 
disableKinds:
- rss
disableKinds = ['rss']
{
   "disableKinds": [
      "rss"
   ]
}
默认情况下,每个订阅源中的项目数量不限。根据需要在站点配置中更改此设置:
hugo.
 
 
 
services:
  rss:
    limit: 42
[services]
  [services.rss]
    limit = 42
{
   "services": {
      "rss": {
         "limit": 42
      }
   }
}
将 limit 设置为 -1 可为每个订阅源生成无限数量的项目。
内置的 RSS 模板将渲染以下值(如果存在)来自您的站点配置:
hugo.
 
 
 
copyright: © 2023 ABC Widgets, Inc.
params:
  author:
    email: [email protected]
    name: John Doe
copyright = '© 2023 ABC Widgets, Inc.'
[params]
  [params.author]
    email = '[email protected]'
    name = 'John Doe'
{
   "copyright": "© 2023 ABC Widgets, Inc.",
   "params": {
      "author": {
         "email": "[email protected]",
         "name": "John Doe"
      }
   }
}
包含订阅源引用
要在渲染页面的 head 元素中包含订阅源引用,请将其放在模板的 head 元素内:
{{ with .OutputFormats.Get "rss" -}}
  {{ printf `<link rel=%q type=%q href=%q title=%q>` .Rel .MediaType.Type .Permalink site.Title | safeHTML }}
{{ end }}
Hugo 将将其渲染为:
<link rel="alternate" type="application/rss+xml" href="https://example.org/index.xml" title="ABC Widgets">
自定义模板
通过创建您自己的一个或多个模板并遵循 模板查找顺序 中所示的命名约定,可以覆盖 Hugo 的 嵌入式 RSS 模板 。
例如,要为首页、栏目、分类法和术语页面使用不同的模板:
layouts/
└── _default/
    ├── home.rss.xml
    ├── section.rss.xml
    ├── taxonomy.rss.xml
    └── term.rss.xml
RSS 模板在上下文中接收 .Page 和 .Site 对象。