Setup Hugo to output only an RSS feed of blog posts

By default, Hugo generates RSS files for every section your site has. These are generated as index.xml files in each section directory (eg post/index.xml, project/index.xml) and list all pages for that section. An index.xml file is also rendered at the root of your project and includes items from all sections of your site.

You can configure Hugo to generate a single RSS file listing only posts from a single section (e.g blog posts). A custom RSS file name can also be set (e.g. feed.xml).

1. Output a single RSS file

To configure Hugo to only output a single RSS file to the root of your project, add the following to your config.toml:

[outputs]
home = [ "RSS", "HTML"]
section  = ["HTML"]

source: Hugo community

2. Set the RSS file to feed.xml

To set a custom name for the RSS file, add the following to your config.toml:

[outputFormats]
[outputFormats.RSS]
mediatype = "application/rss"
baseName = "feed"

You can change the baseName from feed to your preferred file name.

source: Hugo community

3. Set the RSS file at the project root to display only blog posts

Hugo uses a default RSS template to generate the RSS files. The template code is shown below:

 1<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 2  <channel>
 3    <title>{{ if eq  .Title  .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{.}} on {{ end }}{{ .Site.Title }}{{ end }}</title>
 4    <link>{{ .Permalink }}</link>
 5    <description>Recent content {{ if ne  .Title  .Site.Title }}{{ with .Title }}in {{.}} {{ end }}{{ end }}on {{ .Site.Title }}</description>
 6    <generator>Hugo -- gohugo.io</generator>{{ with .Site.LanguageCode }}
 7    <language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
 8    <managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
 9    <webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
10    <copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
11    <lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
12    {{ with .OutputFormats.Get "RSS" }}
13        {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
14    {{ end }}
15    {{ range .Data.Pages }}
16    <item>
17      <title>{{ .Title }}</title>
18      <link>{{ .Permalink }}</link>
19      <pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
20      {{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
21      <guid>{{ .Permalink }}</guid>
22      <description>{{ .Summary | html }}</description>
23    </item>
24    {{ end }}
25  </channel>
26</rss>

The default template uses {{ range .Data.Pages }} to loop over the pages in a section or, for the index.xml at the root, all pages in every section.

To include only your blog posts in the feed copy the default RSS template code to layouts/rss.xml in your Hugo project. Then change line 15 in the new rss.xml file, from:

{{ range .Data.Pages }}

to the following, which limits the range to sections of type post:

{{ range where .Site.RegularPages "Section" "post" }}

You can change "post" to the appropriate section in your site.

Now you have a single RSS file, you can link to it in the html <head> of your template files. To include a link to feed.xml at your site root add the following to your header.html partial or relevant template:

<link rel="alternate" type="application/rss+xml" href='{{ "feed.xml" | absURL }}' />