RSS模板

RSS Templates - RSS模板

https://gohugo.io/templates/rss/

​ Hugo 自带 RSS 2.0 模板,几乎不需要配置,或者您可以创建自己的 RSS 模板。

RSS模板查找顺序

​ 有关完整参考,请参见 Template Lookup Order

​ Hugo 自带了 RSS 2.0 模板。嵌入式模板对于大多数用例已经足够了。

​ RSS 页面属于 Page 类型,并且在模板中可以使用所有 页面变量

Section RSS

section 的 RSS 将在 /<SECTION>/index.xml(例如,https://spf13.com/project/index.xml)处被渲染。

​ Hugo 提供了定义任何 RSS 类型的功能,并且可以为每个章节和分类法设置不同的 RSS 文件。

RSS模板查找顺序表

​ 下表显示了不同页面类型的 RSS 模板查找顺序。第一个列表显示了在使用某一主题(demoTheme)运行时的查找顺序。

ExampleOutputFormat后缀Template Lookup Order
RSS homeRSSxml1. layouts/index.rss.xml
2. layouts/home.rss.xml
3. layouts/rss.xml
4. layouts/list.rss.xml
5. layouts/index.xml
6. layouts/home.xml
7. layouts/list.xml
8. layouts/_default/index.rss.xml
9. layouts/_default/home.rss.xml
10. layouts/_default/rss.xml
11. layouts/_default/list.rss.xml
12. layouts/_default/index.xml
13. layouts/_default/home.xml
14. layouts/_default/list.xml
15. layouts/_internal/_default/rss.xml
RSS section postsRSSxml1. layouts/posts/section.rss.xml
2. layouts/posts/rss.xml
3. layouts/posts/list.rss.xml
4. layouts/posts/section.xml
5. layouts/posts/list.xml
6. layouts/section/section.rss.xml
7. layouts/section/rss.xml
8. layouts/section/list.rss.xml
9. layouts/section/section.xml
10. layouts/section/list.xml
11. layouts/_default/section.rss.xml
12. layouts/_default/rss.xml
13. layouts/_default/list.rss.xml
14. layouts/_default/section.xml
15. layouts/_default/list.xml
16. layouts/_internal/_default/rss.xml
Taxonomy in categoriesRSSxml1. layouts/categories/category.terms.rss.xml
2. layouts/categories/terms.rss.xml
3. layouts/categories/taxonomy.rss.xml
4. layouts/categories/rss.xml
5. layouts/categories/list.rss.xml
6. layouts/categories/category.terms.xml
7. layouts/categories/terms.xml
8. layouts/categories/taxonomy.xml
9. layouts/categories/list.xml
10. layouts/category/category.terms.rss.xml
11. layouts/category/terms.rss.xml
12. layouts/category/taxonomy.rss.xml
13. layouts/category/rss.xml
14. layouts/category/list.rss.xml
15. layouts/category/category.terms.xml
16. layouts/category/terms.xml
17. layouts/category/taxonomy.xml
18. layouts/category/list.xml
19. layouts/taxonomy/category.terms.rss.xml
20. layouts/taxonomy/terms.rss.xml
21. layouts/taxonomy/taxonomy.rss.xml
22. layouts/taxonomy/rss.xml
23. layouts/taxonomy/list.rss.xml
24. layouts/taxonomy/category.terms.xml
25. layouts/taxonomy/terms.xml
26. layouts/taxonomy/taxonomy.xml
27. layouts/taxonomy/list.xml
28. layouts/_default/category.terms.rss.xml
29. layouts/_default/terms.rss.xml
30. layouts/_default/taxonomy.rss.xml
31. layouts/_default/rss.xml
32. layouts/_default/list.rss.xml
33. layouts/_default/category.terms.xml
34. layouts/_default/terms.xml
35. layouts/_default/taxonomy.xml
36. layouts/_default/list.xml
37. layouts/_internal/_default/rss.xml
Term in categoriesRSSxml1. layouts/categories/term.rss.xml
2. layouts/categories/category.rss.xml
3. layouts/categories/taxonomy.rss.xml
4. layouts/categories/rss.xml
5. layouts/categories/list.rss.xml
6. layouts/categories/term.xml
7. layouts/categories/category.xml
8. layouts/categories/taxonomy.xml
9. layouts/categories/list.xml
10. layouts/term/term.rss.xml
11. layouts/term/category.rss.xml
12. layouts/term/taxonomy.rss.xml
13. layouts/term/rss.xml
14. layouts/term/list.rss.xml
15. layouts/term/term.xml
16. layouts/term/category.xml
17. layouts/term/taxonomy.xml
18. layouts/term/list.xml
19. layouts/taxonomy/term.rss.xml
20. layouts/taxonomy/category.rss.xml
21. layouts/taxonomy/taxonomy.rss.xml
22. layouts/taxonomy/rss.xml
23. layouts/taxonomy/list.rss.xml
24. layouts/taxonomy/term.xml
25. layouts/taxonomy/category.xml
26. layouts/taxonomy/taxonomy.xml
27. layouts/taxonomy/list.xml
28. layouts/category/term.rss.xml
29. layouts/category/category.rss.xml
30. layouts/category/taxonomy.rss.xml
31. layouts/category/rss.xml
32. layouts/category/list.rss.xml
33. layouts/category/term.xml
34. layouts/category/category.xml
35. layouts/category/taxonomy.xml
36. layouts/category/list.xml
37. layouts/_default/term.rss.xml
38. layouts/_default/category.rss.xml
39. layouts/_default/taxonomy.rss.xml
40. layouts/_default/rss.xml
41. layouts/_default/list.rss.xml
42. layouts/_default/term.xml
43. layouts/_default/category.xml
44. layouts/_default/taxonomy.xml
45. layouts/_default/list.xml
46. layouts/_internal/_default/rss.xml

配置RSS

​ 默认情况下,Hugo 将创建无限数量的 RSS 条目。您可以通过在项目的 config 文件 中分配数值给 rssLimit: 字段来限制内置 RSS 模板中包含的文章数量。

​ 如果指定以下值,它们也将包含在 RSS 输出中:

config.

=== “yaml”

``` yaml
author:
  name: My Name Here
copyright: This work is licensed under a Creative Commons Attribution-ShareAlike 4.0
  International License.
languageCode: en-us
```

=== “toml”

``` toml
copyright = 'This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.'
languageCode = 'en-us'
[author]
  name = 'My Name Here'
```

=== “json”

``` json
{
   "author": {
      "name": "My Name Here"
   },
   "copyright": "This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.",
   "languageCode": "en-us"
}
```

内嵌的rss.xml

​ 以下是 Hugo 自带的默认 RSS 模板:

https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_default/rss.xml

<head>中引用RSS 订阅

​ 在您的 header.html 模板中,您可以使用 Hugo 的 输出格式<head></head> 标签中指定您的 RSS 订阅,如下所示:

1
2
3
{{ range .AlternativeOutputFormats -}}
    {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
{{ end -}}

​ 如果您只想要 RSS 链接,则可以查询该格式:

1
2
3
{{ with .OutputFormats.Get "rss" -}}
    {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
{{ end -}}

​ 上述两个片段中的任何一个都将为站点首页生成以下 link 标记以用于 RSS 输出:

1
<link rel="alternate" type="application/rss+xml" href="https://example.com/index.xml" title="Site Title">

在本示例中,我们假设 BaseURLhttps://example.com/$.Site.Title"Site Title"

另请参阅

最后修改 May 22, 2023: 第一次提交 (9f24e27)