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.