Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{{#if featured}} does not render post that is both featured and a static page #5898

Closed
willthefirst opened this issue Sep 29, 2015 · 6 comments

Comments

@willthefirst
Copy link

I'm trying to display a featured post that also happens to be a static page:

2015-09-29 at 10 07 am

and using the following code in index.hbs:

{{#foreach posts}}
    {{#if featured}}
        {{content}}
    {{/if}}
{{/foreach}}

Nothing gets rendered, but as soon as I uncheck "Turn this post into a static page", my stuff shows up. This is unexpected behavior.

@willthefirst
Copy link
Author

I've learned that #foreach posts does not search through static pages. I'll try to find another way.

@Kikobeats
Copy link

I have the same issue.

The point is, this is a expected behavior? and if is expected, what strategy use to resolve it?

@willthefirst are you discover something?

@willthefirst
Copy link
Author

@Kikobeats: I worked around it by filtering for posts with a specific tag (in my case, "homepage"), where I know that I should only ever have one post with that tag. In index.hbs

{{#is "home"}}
  {{#foreach posts}}
    {{#has tag="homepage"}}
      {{content}}
    {{/has}}
  {{/foreach}}

@Kikobeats
Copy link

In mi case I discover that I was using a different HTML tag for {{post_class}} helper in post.hbs and page.hbs, so is fixed :-)

@ErisDS
Copy link
Member

ErisDS commented Jan 4, 2016

With the new {{#get}} helper, you can do:

{{#get "posts" filter="featured:true+page:true"}}
  {{#foreach posts}}
  ... do something with all featured pages...
  {{/foreach}}
{{/get}}

The {{#foreach}} helper only loops through the data that is available, so once the post gets old enough to not be shown on page 1, you'll no longer see the post if you use {{#foreach}} + {{#has}}.

The {{#get}} helper does a query to fetch the exact data you want, and then you can loop over it using the {{#foreach}} helper.

In the example above, the query is filter="featured:true+page:true" - "get all posts where featured is true and page is true".

Equally, to get a post with a particular tag you could use: filter="tag:homepage" note that the value for the tag should be the slug value, if you want to use the tag's name, then change to filter="tag.name:'Home Page'".

@lazercaveman
Copy link

lazercaveman commented Sep 4, 2018

The {{get}} helper is the traditional way of doing this.

However there is also the recently added dynamic routing feature which may cover more to stuff like fully separating blog posts...

https://docs.ghost.org/docs/dynamic-routing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants