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 對象。