'Ghost foreach missing post context
I'm trying to access the post published_at date inside a loop that's iterating over the authors of a post but it's not working
This is an example: the date outside of the foreach block is correct but inside the block it will default to the current date
{{date published_at format="D MMMM YYYY"}}
{{#foreach authors}}
<time class="author--date">{{date published_at format="D MMMM YYYY"}}</time>
{{/foreach}}
Solution 1:[1]
Ghost uses Handlebars template syntax, and in your case foreach creates a new scope. So inside it you need to access parent scope to get published_at variable.
By prepending ../ to the property name, you can reference the parent scope. Check docs for more info.
So in your case it should be:
{{date published_at format="D MMMM YYYY"}}
{{#foreach authors}}
<time class="author--date">{{date ../published_at format="D MMMM YYYY"}}</time>
{{/foreach}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KiraLT |
