Setting Up RssOnly Posts in Hugo

Posted by Ryan Himmelwright on Sun, Aug 27, 2023
Tags hugo, website, customization
Downtown Durham NC

Awhile ago, I wanted to write posts that would show up in the website’s rss feed, but not be linked to anywhere else on the site. Unfortunately, this isn’t an option you can simply toggle on in hugo (that I could find). So, I implemented it myself in my theme layout.

Marking a post as rss only

The first thing I needed was a way to mark which pages should be published only in rss. For that, I created a marker which can be added in the header meta data of those posts:

Rssonly = "True"

I don’t think I have to define it anywhere. If it’s in the header of posts, it can be used in the templates.

Adding Home Page Filters

With a way to mark posts as rss-only, I could start adding filters so that those posts wouldn’t show elsewhere on the site. The first place was to remove them from the listing on the homepage, so I went to it’s layout file: /layouts/index.html.

There, I took the following line of code, which grabs pages to list on the home page:

{{ $section := where .Site.RegularPages "Section" "in" $mainSections }}

and replaced it with this one:

{{ $section := where (where .Site.RegularPages "Section" "in" $mainSections) "Params.rssonly" "!=" true}}

The change omits posts that have the rssonly metadata set to true.

Adding Archive Filters

Next, I wanted to remove the rss-only page from appearing on the posts archive page (or any other post lists), so I went to my ’list’ layout at /layouts/_default/list.html.

This time, I modified the .Paginator.Pages to filter out rss-only posts:

{{ range .Paginator.Pages }}

modified to:

{{ range where .Paginator.Pages ".Params.Rssonly" "!=" true }}

Including a message at the top of Rssonly posts

Lastly (so I thought), I wanted to add a special case for the layout to automatically add a message at the top of rss-only pages, to declare they were rss-only. So, I added the following snippet in my /layouts/post/single.html file, right above the {{ .Content }} line:

{{ if .Params.rssonly }}
    RSSONLY MESSAGE                    
{{ end }}

At that point, I thought I was done, so I started writing this post to document and share my work.

Next/Prev post fix

However, as I was writing, I noticed an issue. Posts that were before or after an rss-only post, linked to the “rss-only” post in the next/previous links at the bottom of the page.

I looked into a fix quickly, but wasn’t able to figure out a quick solution at the time. On top of that, the more I looked into solutions, the more I second guessed my method altogether. I probably should have learned how to make a new type of page in hugo, which didn’t show in any of these lists. But that was hard to figure out. Instead, I decided to put the fix and post draft on my backlog for a month or two.

I eventually picked up the problem again and managed to figure out a solution for my prev/next page code (found in /layouts/post/single.html):

<!-- Next Post/ Previous Post Links -->
{{ $pages := where (where .Site.RegularPages "Section" .Section) ".Params.Rssonly" "!=" true }}
{{ if $pages.Next . }}
    <div style="float: right; text-decoration: underline; text-align: right;">Next Post:</div>
{{ end }}
{{ if $pages.Prev . }}
     <div style="float: left; text-decoration: underline; text-align: right;">Prev Post:</div>
{{ end }}
<br>
{{ with $pages.Next . }}
    <a href="{{ .Permalink }}" style="text-align: right; float: right; max-width: 40%;">{{ .Title }}</a>
{{ end }}
{{ with $pages.Prev . }}
    <a href="{{ .Permalink }}" style="text-align: left; float: left; max-width: 40%">{{ .Title }}</a>
{{ end }}

Here, I to added the following line to make a newly filtered list of possible pages:

{{ $pages := where (where .Site.RegularPages "Section" .Section) ".Params.Rssonly" "!=" true }}

Then, I replaced the .PrevInSection and .NextInSection lines using that $pages variable: $pages.Prev and $pages.Next, which seem to work exactaly how I want it.

With that fix, I started to work on this post again 😅.

Conclusion

As far as I am aware, I think that’s it. As I stated above, I’m sure there are many better solutions to enable rss-only posts in hugo. But this worked for me. If you want to do something similar… maybe do a quick search to make sure nothing obvious comes up first. Now I need to figure out how to add some tests to check that this continues to work…

Next Post:
Prev Post:

Quick Jira Card Links in Obsidian Nearly headless VMs Using utmctl