HUGO

Iterate over param from another section

What I have

I have my normal posts & information about authors in the following format:

├── content
│   ├── posts
│   │   ├── post-1.md
│   │   ├── post-2.md
│   ├── authors
│   │   ├── author-1
│   │   │   ├── _index.md
│   │   ├── author-2
│   │   │   ├── _index.md

The _index.md inside each author-x folder has the following params:

---
name: Author-x
photo: 'https://i.pravatar.cc/500?img=3'
twitter: '@user'
---

Each post-x.md has param authors in this format:

## Other params
authors:
  - Author-1
  - Author-2

What I want to achieve

In each post I want to show photo of all authors of that post.

What I have tried

In layouts\posts\single.html , I have:

<!--Author image with links-->
{{ range $key, $value := .Params.authors }}
{{ with site.GetPage (print "authors/" $key) }}
	{{ $author := . }}
	<a class="author-image" href="{{ $author.Permalink }}">
	<img src="{{ $author.Params.photo }}" alt="Author">
	</a>
{{ end }}
{{ end }}

What’s the problem

The images (<img src... />) are not at all generated.

Additional question

As of now I am managing author details using markdown files. Instead if I use a JSON file in data folder, will that be a better option ?

Are you using taxonomies? Have a read here: Taxonomy Templates | Hugo

You can try something like the following (untested) :

{{ range $key, $value := .Params.authors }}
  {{ with site.GetPage (print "authors" $key) }}
    {{ $author := . }}
    {{ $author.Permalink }} 
    {{ $author.Params.photo }}
  {{ end }}
{{ end }}

Yes, I am using taxonomies for authors. Here is my config.toml

baseURL = "https://example.com"
languageCode = "en-us"
title = "MyTitle"
theme = "hugo-theme"

[taxonomies]
  tag = "tags"
  author = "authors"

[permalinks]
  posts = "posts/:filename/"

Hugo version:
hugo v0.86.0-41C6C52E+extended windows/amd64 BuildDate=2021-07-21T09:53:14Z VendorInfo=gohugoio

This didn’t work. No image is generated like before. Thanks for pitching in @pointyfar.

Sorry, typo:

{{ with site.GetPage (print "authors/" $key) }}

You may have to urlize $key. Basically, you want to site.GetPage authors/auth-one

Would be easier to help you if a specific code snippet if you had your code in a repo we can replicate and test on.

I fixed the typo & tried but still not working. Here is the link to my repo. I have implemented your solution in this file.

For checking out the theme please do hugo server --themesDir ../.. inside the theme’s exampleSite folder.

Thanks again for the response.

{{ range $i, $e := .Params.authors }}
{{ with site.GetPage (print "authors/" (urlize $e)) }}
    {{ $author := . }}
    <a class="author-image" href="{{ $author.Permalink }}">
    <img src="{{ $author.Params.photo }}" alt="Author">
    </a>
{{ end }}
{{ end }}

should work

1 Like

That was perfect. Thanks a ton for the help @pointyfar :handshake: :innocent:.

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