Author name from a path inserted in a .md file

Hi, I generated a post via Tina CMS. I created a relationship with the “Authors” collection.
In post.md I have this result:

---
title: Title of the Pos
author: content/author/Paul.md
---
The author "Paul" is:
---
name: Paul
avatar: paul.jpg
---

How can I retrieve the author’s information (name and avatar) on my HTML page?
Sorry, but I’ve only recently become interested in HUGO. Thank you.

Is this really what your markdown looks like?

When you paste code into a forum post, encapsulate with backticks or use the </> button.

```
my code here
```
2 Likes

Firstly, your MD looks as if it contained several front matter sections. That’s not possible, afaik.

Secondly, there are several possibilites to achieve what you want.

  • if you want to have author pages, then putting their MD files in content/author/<authorname.md> is ok
  • otherwise, i.e. if you only need the author data in the HTML generated from the posts, you could put the author data in (for example) JSON files in the data directory and read those.

In any case, you’ll have to process the author data in your single or list templates for the posts. Not in the MD files.

Also, it’s easier to provide the name of the author only in the post’s front matter and have your template resolve the path. Provided, all your authors have unique names.

Thanks.

I have this code in the single.html file:

{{ if ne .Type "page" }}
    <div class="text-sm antialiased opacity-60">
      {{ if .Date }}
      <time>{{ .Date | time.Format ":date_medium" }}</time>
      {{ end }}<!---->
      {{ $single_author := or .Params.Author site.Author.name }}
      
      <!---->
      {{ if $single_author }}
      <span class="mx-1">&middot;</span>
      <span>{{ $single_author }}</span>
      {{ end }}
    </div>
    {{ end }}
  </header>

but what I get is simply:

Sep 29, 2023 · content/author/paul.md

I can’t find a way to get the author name taken from the .md file I have in the “content/author/paul.md” folder

Check out Local file templates | Hugo
But I still think that putting the name of the author in the post is the natural choice. Or using JSON data.

Thanks to you I found this solution:

{{ $single_author := .Params.Author }}

{{ if $single_author }}
<span class="mx-1">&middot;</span>
{{ $authorName := (readFile (printf .Params.Author ) | transform.Unmarshal).name }}
<span>{{ $authorName }}</span>
{{ end }}

Why do you printf the author? I don’t think that’s necessary. And I’d (again) suggest to do something like this

{{ $single_author := .Params.Author }}
{{$path := "content/author"}}
{{ if $single_author }}
<span class="mx-1">&middot;</span>
{{ $authorName := (readFile (printf "%s%s.md“ $path $single_author ) | transform.Unmarshal).name }}
<span>{{ $authorName }}</span>
{{ end }}

That permits to centralize the location of the author files. If you ever want to move them elsewhere, only one file to modify.

And perhaps add a class to the author span so it’s easier to style.

Thanks.

I don’t print the author because I’m using “tina cms”. In the CMS I have a type of “reference” field that prints the author’s path in the post.md. I should find a way to change it.