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:

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:

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="">
 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 --</generator>{{ with .Site.LanguageCode }}
 7    <language>{{.}}</language>{{end}}{{ with }}
 8    <managingEditor>{{.}}{{ with $ }} ({{.}}){{end}}</managingEditor>{{end}}{{ with }}
 9    <webMaster>{{.}}{{ with $ }} ({{.}}){{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 }}<author>{{.}}{{ with $ }} ({{.}}){{end}}</author>{{end}}
21      <guid>{{ .Permalink }}</guid>
22      <description>{{ .Summary | html }}</description>
23    </item>
24    {{ end }}
25  </channel>

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 }}' />