How can I get the author from the permalink to use as a variable?

Going one step further than the answer here https://discourse.gohugo.io/t/list-one-authors-posts/29055 and using the function below to list a specific authors’ posts on an author’s bio page:

config.toml:
[taxonomies] author = "authors"

/content/authors/arthur-conan-doyle/_index.md contains:
author: "Arthur Conan Doyle

{{ $author := site.GetPage “authors/arthur-conan-doyle” }}
{{ range first 6 (where $author.Pages “.Type” “posts” ) }}
{{ .Title | truncate 50 }}
{{- end }}

How can I dynamically get the author name from the permalink and use it something like authors/$current_author so that I can include the function above on any author template page without having to hardcode the author (i.e. “arthur-conan-doyle”) in template files?

Pull it from front matter, like you do with .Title - so the author name is set in the front-matter.

Your alternative is to get cute with string parsing on the url. . . but there are multiple potential problems with that - this is what front-matter is for.

That’s a good idea, as {{ .Params.author }} already works to get the dynamic page title from /content/authors/my-author/_index.md`.

But how would I pull the author into a page listing function? Using $author := .Params.author obviously doesn’t work:

{{ $author := .Params.author }}
	{{ range first 6 (where $author.Pages ".Type" "posts" ) }}
       <li>
        <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
       </li>
 {{- end }}

You’re almost there. . .

Remember when you get the author name this is a title, it’s not actually the collection of pages you want to range through.

{{ $authorPage := site.GetPage "authors/arthur-conan-doyle" }}
{{ $authorName := $authorPage.Params.author }}
{{ range first 6 (where $authorPage.Pages ".Type" "posts" ) }}
       <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
{{- end }}

Ah, I see. But "site.GetPage "authors/arthur-conan-doyle" is still the problem; I want the function to not require a hardcoded author slug like authors/arthur-conan-doyle. Can $authorPage be generated from Params.author?

Or, I could add each author slug as a Params in each _index.md, but that defeats the purpose.

Do I understand correctly that this is for a sidebar on something like an article/blog page? And the sidebar should list other articles by the same author?

Check out the docs here for something very similar: https://gohugo.io/templates/taxonomy-templates/#example-comma-delimit-tags-in-a-single-page-template.

This example assumes your content page has front-matter field “author”, which going by your example would be set to “arthur-conan-doyle”

    {{ $taxo := "authors" }}
    {{ $authorName := .Params.author }}
    {{ $authorPage := $.Site.GetPage (printf "/%s/%s" $taxo $authorName) -}}
    {{ range first 6 (where $authorPage.Pages ".Type" "posts" ) }}
      <li>
        <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
      </li>
    {{- end -}}

All of that being said, the .GetPage construction is relatively brittle, I would maybe look into using .GetTerms with a where clause to get at the taxonomy term data you want. It’s documented here: https://gohugo.io/templates/taxonomy-templates/#example-list-tags-in-a-single-page-template and https://gohugo.io/functions/where/

This way will allow you to use have “Arthur Conan Doyle” in the content page’s author field, and on the /authors/arthur-content-doyle/_index.md page also have author field “Arthur Conan Doyle”, so this same author field is what serves as the link or common identifier between the pages.

{{ $authorName := .Params.author }}
{{ range where (.GetTerms "authors") "author" $authorName }}

Warning: I’ve not actually run this, so you’re going to have to adapt to work with your setup. I’m assuming the taxonomy page has a front-matter field “author” that contains the author name.

This is for an author’s bio page, not a sidebar or single, using the structure /content/authors/arthur-conan-doyle/_index.md and /layouts/author/lists.html. Your example doesn’t return anything; I’ll keep digging or use a Params for the slug.

do you mean that you’re on the author’s taxonomy page /content/authors/arthur-conan-doyle/_index.md and you just want to list all the pages belonging to that taxonomy term (the author, in this case)?

if this is the case we’ve massively been over-complicating things - just use the standard taxonomy list template. https://gohugo.io/templates/lists/#taxonomy-template

So in /layouts/author/lists.html:

<h1>{{ .Title }}</h1>
<ul>
{{ range first 6 (where .Pages ".Type" "posts" ) }}
  <li>
    <a href="{{ .RelPermalink }}">{{ .Title | truncate 50 }}</a>
  </li>
{{ end }}
</ul>

In this case .Title is whatever the page title is - which is likely to be your author name.

Thanks! And yes, we were overthinking it, and the answer to my original question here https://discourse.gohugo.io/t/list-one-authors-posts/29055 is more complex than need be.

Your version
{{ range first 6 (where .Pages ".Type" "posts" ) }}
works on /layouts/author/lists.html.

And for use in a sidebar, this works:
{{ range first 6 (where .Site.RegularPages ".Type" "posts" ) }}

1 Like

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