Can't loop through posts on page other than index

I have this code here:

{{!< default}}
<div class="ms-index-container">
    <div></div>
    <div class="ms-postfeed">
        {{#foreach posts}}
            {{#if featured}}
                 <a href="{{url}}" class="ms-postcard">
                    <img src="{{img_url feature_image}}">
                    <h3>{{title}}</h3>
                    <p>{{date}}</p>
                </a>
            {{/if}}
        {{/foreach}}
    </div>
    <div></div>
</div>

And it works fine on my index.hbs, however on any other page it doesnt work. I just get blank nothing as if there are no posts.

On pages outside the index.hbs, I use the get helper to fetch posts, pages, and tags. For example, you’d wrap the #foreach helper with the get helper like so:

{{#get "posts"}}
    {{#foreach posts}}
        {{title}}
    {{/foreach}}
{{/get}}

I recently learned that using too many of these on one template can slow your page speed down, so you have to use them strategically.

1 Like