TEMPLATES

RSS templates

設定

預設情況下,當你構建網站時,Hugo 會為首頁、分類頁、分類法頁面和術語頁面生成 RSS 訂閱源。你可以在站點設定中控制訂閱源的生成。例如,要生成首頁和分類頁的 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: 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 模板

例如,若要為首頁、分類頁、分類法頁面和術語頁面使用不同的模板,可以這樣結構你的 layouts 目錄:

layouts/
└── _default/
    ├── home.rss.xml
    ├── section.rss.xml
    ├── taxonomy.rss.xml
    └── term.rss.xml

RSS 模板會在上下文中接收 .Page.Site 物件。