Posts data inside Author context is empty

I’m running a locally installed version of Ghost 3.14, working on the author.hbs template. I’ve found that following the instructions here:

https://ghost.org/docs/api/v3/handlebars-themes/context/author/

I get all of the author data just fine, but using a {{#foreach “posts”}} returns an array of five empty items. For example, in my template:

{{#author}}
...author information here...
<h2>Posts by {{name}}</h2>
{{#foreach "posts"}}
  <p>Title: {{title}}</p>
{{/foreach}}

Returns the following HTML:

<h2>Posts by author</h2>
<p>Title:</p>
<p>Title:</p>
<p>Title:</p>
<p>Title:</p>
<p>Title:</p>

I deleted all of the starter posts, and there are definitely more than 5 posts in my installation.

I do have a modified routes.yaml file - could that have something to do with the issue?

Thanks!

routes.yaml

routes:
  /signup/: members\signup
  /signin/: members\signin
  /account/: members\account
  /support/: members\support

collections:
  /:
    permalink: /{slug}/
    template: index
    filter: tags:-[comic,shop,newsletter]
  /comic/:
    permalink: /comic/{slug}/
    template: comic
    filter: primary_tag:comic
  /shop/:
    permalink: /shop/{slug}/
    template: shop
    filter: primary_tag:shop

taxonomies:
  tag: /tag/{slug}/
  author: /author/{slug}/

Hey @dan.2rational :wave:

Could you try writing out the foreach loop without the quotes? Like so:

{{#foreach posts}}
  <p>Title: {{title}}</p>
{{/foreach}}

This looks like a typo in our docs. We’ll get this amended asap!

@DavidDarnes Thanks for the quick reply!

That causes the loop to return nothing. All I get is the h2 element.

Thanks again!

Oh really? In your example the closing {{/author}} is missing, do you have that in your template?

I do - everything else in the template renders correctly.

That does beg a question - should the {{#foreach}} loop be INSIDE the {{#author}} tags?

Oops yep you’re right. In Casper it’s after the author context:

That got it! Thank you!

Just for clarification - is it the author.hbs template that somehow filters the posts by author? Because it does filter by author!

Last question - if I want to filter the list of returned posts for the author, does the foreach helper accept a filter? I tested it:

{{#foreach posts filter="tags:-shop"}}

But that still returns posts that are products in our shop that have a primary tag of ‘shop’ (integration with Shopify that works REALLY nicely!).

Thanks again!

You’ll be better off using the #get helper within the context of the author so you can fine tune it a bit more. Here’s an example:

  {{#get "posts" filter="authors:{{slug}}+tags:-shop" limit="all"}}
      {{#foreach posts}}
          <li><a href="{{url}}">{{title}}</a></li>
      {{/foreach}}
  {{/get}}
{{/author}}
1 Like

Excellent! Thank you so much!

Just for posterity sake, the solution is different depending on what one wants to do.

To lists posts by an author on author.hbs, use the marked solution (#foreach helper outside the #author context).

To filter the posts returned, it’s necessary to use the #get helper, but this must be done inside the #author context:

{{#author}}
  ...author information here...
  <h2>Posts by {{name}}</h2>
  {{#get "posts" filter="author:{{slug}}+tags:-[tags to filter here]" limit="all"}}
    {{#foreach "posts"}}
      <p>Title: {{title}}</p>
    {{/foreach}}
  {{/get}}
{{/author}}

This way, the {{slug}} works inside the filter. :slight_smile:

1 Like

Appreciate the clarity and sharing your findings in full @dan.2rational :raised_hands: