List authors from one section #taxonomy

How can I list the authors who have at least one article in the content/articles section of my website ? The difficulty I encounter is to avoid listing authors from only other sections, e.g. content/books.

Currently, I have the following code:

    {{- with (.Site.GetPage "taxonomyTerm" "authors") -}}
      {{ $authors := .Pages }}
      {{- range $i, $author := $authors -}} {{/* Trick from https://discourse.gohugo.io/t/howto-delimiter-separated-tags/146 */}}
          {{- $NArticles := len (where $author.Pages "Section" "articles") -}}
          {{- if ge $NJointArticles 1 -}}
            {{- if $i -}}, {{ end }} 
            <a href="{{ $author.Params.website }}">{{- $author.Title -}}</a>
          {{- end -}}
      {{- end -}}
    {{end}}

It works, but this is not very satisfactory. I think that if the first author (alphabetically) is to be excluded because he did not author in the content/articles section, my comma separated list will be messed up and it will start by a comma.

only do the comma after the 1st iteration:
{{- if gt $i 0 -}}, {{ end }}

@yaythomas both code snippets are true, because $i is zero in the first iteration. The problem is, that it’s not counting based on the if-loop but based on the range-item.

@gillesbonnet have a look at Scratch. You can set a variable that counts the number of authors with one or more posts (inside of the {{- if ge $NJointArticles 1 -}} loop. and then AFTER that loop but still inside the range you check that scratch. If it’s larger than 0 then add a comma.

1 Like

I just realized that this will add a comma after the last author. Have a look at this comment to see how to check if we are at the last item:

Thank you @davidsneighbour. Indeed, it should work with Scratch. I though I was missing something simpler, more direct, like using where. Unfortunately I was not successful with this latter option.

Thank you @yaythomas , but as @davidsneighbour said, I think these two codes are equivalent. The problem lies somewhere else. See the comment of @davidsneighbour. Forgive me for not making this clear enough in the first place.

no, my bad, I didn’t pay enough attention to what you were actually asking!

you could use a range with a where, like you mentioned? What was your trouble with it?

it would look something like this:

    {{ $authorsInSection := where $authors "Section" "articles" }}
    {{ range $i, $author := $authorsInSection }}
        {{- if gt $i 0 -}}, {{ end }}{{- $author.Title -}}
    {{ end }}

Thanks @yaythomas for coming back to me. Actually I tried this before but then $authorsInSection is empty. I can see that because if I write {{ $authorsInSection }} it prints on my website Pages(0).

Thanks again @yaythomas and @davidsneighbour for your suggestions. At the end I found an alternative way, which I described in a Tip and trick post.

This is the solution of a question I asked on that forum.

Context: You have two sections content/articles and content/books. You also set up a taxonomy for the authors with additional info for each author under content/authors/.

Now you want to list all authors who posted one or multiple articles and add a link to their corresponding webpages. With .Site.GetPage "taxonomyTerm" "authors" you get access to the list of all the authors in your taxonomy (and all the attached information). But this includes also the authors of other sections of your website. I tried

{{ $authorsInSection := where $authors "Section" "articles" }}

but this did not work (It creates Pages(0)). The alternative way I found is to check for each author whether her or his number of articles len (where $author.Pages "Section" "articles") is at least one.
Attempting to make this a bit clean, I start by creating a slice which selects all authors in the section I consider (here articles) and then range through that slice, with a little trick to separate the names by commas.

{{- with (.Site.GetPage "taxonomyTerm" "authors") -}}
    {{ $authors := .Pages }}
      {{ $authorsInSection := slice }}
      {{- range $author := $authors -}}
        {{- $NArticles := len (where $author.Pages "Section" "articles") -}}  
        {{- if ge $NArticles 1 -}}
          {{ $authorsInSection = $authorsInSection | append . }}
        {{- end -}}
      {{- end -}}

      {{- range $i, $author := $authorsInSection -}}
        {{- if $i -}}, {{ end }} 
        <a href="{{ $author.Params.website }}">{{- $author.Title -}}</a>
      {{- end -}}
{{end}}

This works with me. I could imagine that there exists a more direct way. If someone here knows about it, I’ll be happy to read about it.

Hi @gillesbonnet

I’ve merged your separate post into here. It makes more sense to keep the question and the answer together in the same topic.

glad you got it working!

Doing it more directly with a where+range, where $authorsInSection is empty for you, this is going to be because the $authors collection isn’t right - which is going to have something to do with the .GetPage call.

Without knowing exactly your taxonomy, but something like this may help:

{{ $authors := .Site.Taxonomies.authors }}
{{ $authorsInSection := where $authors "Section" "articles" }}
{{ range $i, $authorPage := $authorsInSection }}
  {{ $author := $authorPage.Page }}
  {{- if gt $i 0 -}}, {{ end }}{{- $author.Title -}}
{{ end }}

Great that works! When I see how simple it is at the end, I wonder why I could not find this myself ! Anyway… Thanks a lot. :slight_smile:

Now, the weird thing is that {{- if gt $i 0 -}}, {{ end }} does not work and I have no idea why. No comma is shown.

Is it inside of the loop that creates $i? Maybe post your current code.

Ah I found out something! In this context $i does not range through the numbers 0, 1, 2, ... but through the authors firstauthor-Name-Surname, secondauthor-Name-Surname, … So these are character strings. It just does not make much sens to compare them to 0.

So, I found out where the (new) problem comes from :slight_smile: . But I have no idea how to solve it :thinking:.

@davidsneighbour My current code is exactly as @yaythomas suggested with the only modification of having <a href="{{ $author.Params.website }}">{{- $author.Title -}}</a> instead of only {{- $author.Title -}}.

ah yes, of course, the collection is an map object, not a slice. . .

there’s a few ways of solving this - mostly simply just with a boolean. but if you want to keep an iterator for some reason you can do this:

{{ $authors := .Site.Taxonomies.authors }}
{{ $authorsInSection := where $authors "Section" "articles" }}
{{ $i := 0 }}
{{ range $authorPage := $authorsInSection }}
  {{ $author := $authorPage.Page }}
  {{- if gt $i 0 -}}, {{ end }}{{- $author.Title -}}
  {{- $i = add $i 1 -}}
{{ end }}
2 Likes

Thanks @yaythomas. Can I really write it as you do ? Don’t I need a scratch for changing the value of $i inside the range?

Hugo introduced recently (some weeks/months ago) features that make this possible. It should work with the latest version.

2 Likes

Very good ! Indeed, it was time to upgrade my Hugo :slight_smile:
I think everything is fine now. Thanks again @davidsneighbour and @yaythomas for your precious help !

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