When managing a Hugo static site, you might encounter situations where you want certain posts to appear on your website but not in your RSS feed. Perhaps you’re publishing internal announcements, draft previews, or content that’s only relevant to direct website visitors. Hugo doesn’t provide this functionality out of the box, but with a simple modification to your RSS template, you can gain granular control over what appears in your feed.
The Challenge
By default, Hugo includes all published posts in your RSS feed. While you can use the draft: true
parameter to exclude content entirely, this removes the post from both your site and RSS feed. What if you want more nuanced control?
The Solution: Custom RSS Filtering
The solution involves modifying your RSS template to filter out posts based on custom front matter parameters. Here’s how to implement it:
Step 1: Modify Your RSS Template
First, locate your RSS template file. This is typically found at layouts/_default/rss.xml
in your Hugo site. If you don’t have a custom RSS template, you can create one by copying Hugo’s default RSS template.
Add filtering logic to exclude posts with specific parameters:
|
|
The key lines are:
{{- $pages := where $pages "Params.hidden" "!=" true -}}
- Excludes posts withhidden: true
{{- $pages = where $pages "Params.exclude_from_rss" "!=" true -}}
- Excludes posts withexclude_from_rss: true
Step 2: Using the Exclusion Parameters
Now you have two ways to control RSS inclusion:
Option 1: Complete Hiding
Add to your post’s front matter to hide from both site and RSS:
|
|
Option 2: RSS-Specific Exclusion
Add to your post’s front matter to exclude only from RSS:
|
|
Real-World Use Cases
This functionality is particularly useful for:
- Internal announcements - Company updates that don’t need to reach external RSS subscribers
- Draft previews - Sharing work-in-progress content with select audiences
- Seasonal content - Holiday messages or temporary notices
- Meta content - About pages, privacy policies, or site maintenance notices
- Testing content - Posts used for design or functionality testing
Complete RSS Template Example
Here’s a complete, minimal RSS template with the filtering logic:
File Path:
layouts\_default\rss.xml
|
|
Testing Your Implementation
After implementing the changes:
- Build your site: Run
hugo
to generate the static files - Check the RSS feed: Look at
yourblogonthecloud/rss.xml
to verify excluded posts aren’t present - Validate the XML: Ensure the RSS feed is still valid XML
- Test with RSS readers: Verify the feed works correctly with RSS readers
Alternative Approaches
While front matter filtering is the most flexible approach, you could also:
- Use sections: Create a separate content section for RSS-excluded content
- Use taxonomies: Filter based on specific categories or tags
- Use build conditions: Conditionally include content based on build parameters
Conclusion
With this simple modification to your Hugo RSS template, you gain fine-grained control over what content appears in your RSS feed. This flexibility allows you to serve different audiences appropriately - keeping your website comprehensive while maintaining a focused, relevant RSS feed for subscribers.
The beauty of this approach is its simplicity and non-intrusive nature. Existing posts remain unaffected, and you only need to add exclusion parameters when you specifically want to control RSS visibility.