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.
@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.
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.
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).
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.
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 }}
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 . But I have no idea how to solve it .
@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 -}}.
Very good ! Indeed, it was time to upgrade my Hugo
I think everything is fine now. Thanks again @davidsneighbour and @yaythomas for your precious help !