Can I list related content myself?

I would like to use hugo’s related content feature, but I’d like to specify myself, in the frontmatter for each page, the related pages for that page. I don’t need any kind of automatic detection of related content, but I do need a convenient way to iterate through related pages from within the layout templates. Is there a way I can do this within hugo’s related content system, or should I do it a different way?

If you manually add a list with related posts in the frontmatter you can then range through that param in your template.

2 Likes

Building on @frjo’s comments…

We do this in some sections of the Hugo documentation site, where the front matter contains an array of page paths, not an array of URLs. Then we can do something like:

{{ range .Params.related }}
  {{ with site.GetPage . }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
  {{ else }}
    {{ errorf "Unable to find related page %s: see %s" . $.Path }}
  {{ end }}
{{ end }}

Using page paths, and then getting each page, is the idiomatic way to handle this.

1 Like

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