Hugo RSS 模板
使用嵌入式 RSS 模板,或创建您自己的模板。
配置
默认情况下,当您构建网站时,Hugo 会为 home、section、taxonomy 和 term 页面生成 RSS 订阅源。在站点配置中控制订阅源生成。例如,要为 home 和 section 页面生成订阅源,但不为 taxonomy 和 term 页面生成:
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"
]
}
}
要禁用所有 页面种类 的订阅源生成:
disableKinds:
- rss
disableKinds = ['rss']
{
"disableKinds": [
"rss"
]
}
默认情况下,每个订阅源中的项目数量是无限的。在站点配置中根据需要更改此设置:
services:
rss:
limit: 42
[services]
[services.rss]
limit = 42
{
"services": {
"rss": {
"limit": 42
}
}
}
将 limit 设置为 -1 以生成无限数量的每个订阅源的项目。
内置 RSS 模板将渲染以下值(如果存在)来自您的站点配置:
copyright: © 2023 ABC Widgets, Inc.
params:
author:
email: jdoe@example.org
name: John Doe
copyright = '© 2023 ABC Widgets, Inc.'
[params]
[params.author]
email = 'jdoe@example.org'
name = 'John Doe'
{
"copyright": "© 2023 ABC Widgets, Inc.",
"params": {
"author": {
"email": "jdoe@example.org",
"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 模板。例如,要对 home、section、taxonomy 和 term 页面使用不同的模板:
layouts/
├── home.rss.xml
├── section.rss.xml
├── taxonomy.rss.xml
└── term.rss.xmlRSS 模板在上下文中接收 .Page 和 .Site 对象。