Best way to set <uri> for each of multiple entry coauthors in Atom feed

Hi all, big Hugo fan. Hope you can help: I’ve been tearing my hair out about how even to approach this.

What I want is to be able to include <uri> values for blog post coauthors in the Atom feed I’ve set up for my site. Here’s an example of my desired output in index.atom.xml for a sample <entry> with two coauthors:

<author>
	<name>Richard Hendricks</name>
	<uri>https://piedpiper.com</uri>
</author>
<author>
	<name>Nelson Bighetti</name>
	<uri>https://stanford.edu</uri>
</author>

Right now I’ve set up coauthors by setting an authors taxonomy and calling the authors with .Params.authors, and I can easily set up individual author pages with metadata for each author term that includes that author’s <uri>, but I can’t figure out how to attach that metadata to the correct authors in each post. I don’t particularly need individual author pages in any case, so I’m not married to the authors taxonomy.

I suspect that a better approach is either 1) to add author data in the /data directory and draw on that or 2) to hard code the author maps into the front matter TOML of each post, but I’ve had trouble with both of those.

Have consulted Multiple Authors Support in Hugo Website - Coding N Concepts but that only works for multiple authors on a site, not on a post. I’ve figured out how to put one or more author data files in /data and display that data for posts with a single author, but not how to say, in essence, “Find each of the authors of this post in the /data directory and display the corresponding uri value for that author.”

Have also read posts like Maps in front matter? and that syntax doesn’t break anything, but I can’t figure out how to call each author from that map.

Even advice about which approach is best and where to start reading would help!

Repo: GitHub - ror-community/ror-site: ROR main site
Site: https://ror.org
Atom feed: Research Organization Registry (ROR)

Create an authors taxonomy in your site configuration

[taxonomies]
author = 'authors'

Create the term pages

content/authors/
├── adent/
│   └── _index.md
├── fprefect/
│   └── _index.md
└── zbeeblebrox/
    └── _index.md

Populate the term pages

content/authors/adent/_index.md

+++
title = 'Arthur Dent'
[params]
uri = 'https://example.org/adent/'
+++

Reference the authors in front matter

content/posts/post-1.md

+++
title = 'Post 1'
date = 2025-02-03T11:34:54-08:00
draft = false
authors = ['adent','fprefect']
+++

Override embedded RSS template

mkdir -p layouts/_default
touch layouts/_default/rss.xml

Then paste this into the file you just created:

layouts/_default/rss.xml
{{- $pctx := . }}
{{- if .IsHome }}{{ $pctx = .Site }}{{ end }}
{{- $pages := slice }}
{{- if or $.IsHome $.IsSection }}
{{- $pages = $pctx.RegularPages }}
{{- else }}
{{- $pages = $pctx.Pages }}
{{- end }}
{{- $limit := .Site.Config.Services.RSS.Limit }}
{{- if ge $limit 1 }}
{{- $pages = $pages | first $limit }}
{{- end }}
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>{{ if eq .Title .Site.Title }}{{ .Site.Title }}{{ else }}{{ with .Title }}{{ . }} on {{ end }}{{ .Site.Title }}{{ end }}</title>
    <link>{{ .Permalink }}</link>
    <description>Recent content {{ if ne .Title .Site.Title }}{{ with .Title }}in {{ . }} {{ end }}{{ end }}on {{ .Site.Title }}</description>
    <generator>Hugo</generator>
    <language>{{ site.Language.LanguageCode }}</language>{{ with $authorEmail }}
    <managingEditor>{{.}}{{ with $authorName }} ({{ . }}){{ end }}</managingEditor>{{ end }}{{ with $authorEmail }}
    <webMaster>{{ . }}{{ with $authorName }} ({{ . }}){{ end }}</webMaster>{{ end }}{{ with .Site.Copyright }}
    <copyright>{{ . }}</copyright>{{ end }}{{ if not .Date.IsZero }}
    <lastBuildDate>{{ (index $pages.ByLastmod.Reverse 0).Lastmod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
    {{- with .OutputFormats.Get "RSS" }}
    {{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
    {{- end }}
    {{- range $pages }}
    <item>
      <title>{{ .Title }}</title>
      <link>{{ .Permalink }}</link>
      <pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
      {{- range .GetTerms "authors" }}
        <author>
          <name>{{ .Page.Title }}</name>
          {{ with .Page.Params.uri }}<uri>{{ . }}</uri>{{ end }}
        </author>
      {{- end }}
      <guid>{{ .Permalink }}</guid>
      <description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
    </item>
    {{- end }}
  </channel>
</rss>

Try it

git clone --single-branch -b hugo-forum-topic-53373 https://github.com/jmooring/hugo-testing hugo-forum-topic-53373
cd hugo-forum-topic-53373
hugo server

Reference

https://gohugo.io/methods/page/getterms/

3 Likes

Oh that’s fantastic, thanks so much! Works like a charm. Wow, I was really overthinking it. GetTerms is a great new tool for my toolbox.

This bit in particular is exactly what I needed:

{{- range .GetTerms "authors" }}
        <author>
          <name>{{ .Page.Title }}</name>
          {{ with .Page.Params.uri }}<uri>{{ . }}</uri>{{ end }}
        </author>
{{- end }}

And now I can display author metadata in my single layout, too. Awesome.

1 Like

Glad I could help. I’m not sure why, but the GetTerms method is overlooked more often than I’d expect. It’s been around for quite a while.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.