Building a page of articles by a single author

Everything I need to complete the authors section I’m building is complete except for producing an author page that contains details about the author followed by a list of the posts made by that author.

  1. I have the following folders.

\content\authors
\layouts\authors\static\images\authors

  1. Each author will have a folder under \content\authors and, in that folder, a file named _index.md:

\content\authors\john-smith_index.md

Corresponding images will be in the static folder:

\static\images\authors\john-smith.jpg

  1. \layouts_default has two files:

author.terms.html
author.html

  1. In config.toml, my taxonomies are defined as:

[taxonomies]
tag = “tags”
category = “categories”
topic = “topics”
social = “social”
author = “authors”

  1. In an author’s _index.md folder, the front matter looks like this (simplified for example):

title: “John Smith, Ph.D.”
author: John-Smith
image_name: “taxonomy”
image : “images/authors/john-smith.jpg”

  1. In each article, the front matter contains (simplified for example:

title: "How to Take a Walk "
author: [“John Smith”]
image : “images/walks/how-to-take-a-walk.jpg”
type: “post”

The Problem:

My authors page works fine and lists all of the authors in a nice format. I’m happy with that.
It successfully links to the author page, as do the author tags on each post.

The author’s page looks good. However, I want a list of that author’s articles at the bottom of the author’s page and I can’t get that working. I’ve looked at every one of the topics here that refer to authors and authors pages and I’ve tried lots of different things but the end result is always the same: no articles listed.

My current attempt in author.html looks like this:

      {{ $author := .Params.author }}
      <h2>Articles by {{ $author }} </h2>
      {{ range .Site.RegularPages }}
      {{- range where .Site.Pages "Params.authors" "intersect" (slice $author) -}}
        <p>{{ .Params.title }} </p>
        <p> this is an article </p>
      {{- end -}}
      {{ end -}}

It prints the title correctly. It currently says “Articles by John-Smith” because I wanted to see what value was being placed in $author. Earlier, I used the title from the author _index.md and that worked just fine.

I added the “

this is an article

” line just to know if the loop was actually looping through anything. The answer is nope.

I have a feeling that this a really stupid error with a simple fix but, for the life of me, I don’t see it.

Thanks in advance for any help. :slight_smile:

Brian

I found part of the problem. In the article front matter,

author: [“John Smith”]

should have been

authors: [“John Smith”]

Progress. Now I’m getting that article title repeated for every article in our site so I need to find out why it’s ranging over every page instead of just the ones with the selected author.

I figured it out. :slight_smile:

There were two problems:

(1) The various article md files had authors variously listed as “author: John Smith”, “authors: John Smith”, “authors: [“John Smith”]”, etc. Fixing and standardizing the data corrected a great deal of the problem.

(2) I didn’t need two range statements. Once the data was correct, all I needed was:

      {{ $author := .Params.author }}
      <h2>Articles by {{ $author }} </h2>
      {{ range where .Site.RegularPages  "Params.authors" "intersect" (slice $author) }}
          <p>{{ .Title }} Written by {{ .Params.authors }}</p>
      {{ end -}}

One last update: once I took out the test data, I realized the above solution was not quite correct. To fix it, I did:

  1. Changed the author’s _index.md by changing

author: John Smith

to

authors: [“John Smith”]

  1. Changed the author.html logic to this:

      {{ $authors := .Params.authors }}
      <h2>Articles by {{ $authors }} </h2>
      {{ range where .Site.RegularPages  ".Params.authors" "intersect" $authors }}
          <p>{{ .Title }} Written by {{ .Params.authors }}</p>
      {{ end -}}
    

It also works fine with multiple authors on a particular article.