I have a site that includes a blog. Each blog post has an authors:
array as part of the yaml. I also have a the-team
section with individual markdown files for each author. I intentionally did not use taxonomies because I wanted to featured various types of structured information about the team member (ie, sometimes an author) and not just show a list of posts that the author had written.
In my layouts/the-team/single.html
I have the following, which works like a charm but has shortcomings:
{{$theAuthor := .Title }}
<h3 class="contributions">Recent Contributions</h3>
<ul class="recent-contributions">
{{range .Site.Pages }}
{{if in .Params.authors $theAuthor }}
<li><a href="{{.Permalink}}"><i class="icon-{{.Section}}"></i><span class="contribution-title">{{.Title}}</span> <span class="contribution-pubdate"> {{.PublishDate.Format "2006-01-02"}}</span></a></li>
{{end}}
{{end}}
</ul>
The problem is that some of the team members need their own page but have not necessarily written any posts. With the current solution, team members who have not written any posts still get the “Recent Contributions” headline and an empty <ul>
. I’ve tried a half dozen or so combinations of where
and range
but can’t wrap my head around it. Here are the steps that I need done in the template:
- Range over all site pages
- Check the site pages
authors
array - If in the
authors:[]
array,.Title
is present (since.Title
is the author’s name for the individual author page), add the matching page to an array - Check that the length of this array is greater than 0 (ie, the individual team-member/author page has at least one post/page written on the site)
- If greater than 0, add the “Recent Contributions” headline plus the unordered list with each of the posts where the team-member/author has been given attribution
I’ve tried something like the following but with a few different permutations:
{{$contributions := range where .Site.Pages (where in .Params.authors $theAuthor)}}
OR
{{$contributions := range where .Site.Pages (if in .Params.authors $theAuthor)}}
OR
{{$contributions := range where .Site.Pages (where in .Params.authors $theAuthor)}}