Gavin Wray

Limit Hugo's RSS feed to recent content in a single directory

Hugo’s default RSS template publishes an unlimited number of items and shows a summary rather than full content. Also, the feed URL and the content Hugo generates vary depending on the active page’s content type: post, page, section, category or tag.

For this site, I publish one RSS feed with the following attributes:

  • full content so that readers do not have to leave their RSS client
  • most recent 15 posts in the /blog directory
  • my name as the feed title
  • the descriptive URL /feed.xml

To create a similar feed for your Hugo site, follow these steps:

  1. Create a new file in your theme directory at:

     /layouts/_default/rss.xml
    
  2. Copy the Hugo RSS template and paste it in rss.xml.

  3. In my site’s config file, I declare my name in the author parameter. To use the same parameter as the feed title, in rss.xml change the feed’s <title> tag to:

     <channel>
     ...
       <title>{{ $.Site.Params.author }}</title>
     ...
     </channel>
    
  4. For consistency with the feed title, simplify the channel’s <description>:

     <channel>
     ...
       <description>Recent blog posts by {{ $.Site.Params.author }}</description>
     ...
     </channel>
    
  5. By default, Hugo publishes all items in the feed:

     {{ range .Data.Pages }}
    

    To show the most recent 15 items and to constrain items to those in one directory, for example, /blog, change the range to:

     {{ range first 15 (where .Data.Pages "Section" "blog") }}
    
  6. To show the item’s full content in the feed, change the item’s <description> to:

     <item>
     ...
       <description>{{ .Content | html }}</description>
     ...
     </item>
    
  7. Hugo renders the feed at /index.xml by default. You can specify your own URL in your config file. For example, feed:

     [outputs]
       home = [ "HTML", "RSS" ]
    
     [outputFormats]
     [outputFormats.RSS]
       mediatype = "application/rss"
       baseName = "feed"
    

Here’s a preview of the feed in the feedly RSS client:

Recent items shown in feedly

Content © 2008-2019 Gavin Wray
Athena theme by Diana Mounter

Twitter · LinkedIn · SoundCloud · GitHub