I am developing a Ghost template for my blog. And I want to see on the front page only one newest post from posts with specific tag (eg "news").
Is it possible to filter posts by tag in foreach
loop?
Any other solution? (the first thing that comes to mind is some trick with custom template tag-news.hbs
and URL rewriting)
You can definitely filter posts by tag using the {{has}}
helper:
{{#foreach posts}}
{{#has tag="news"}}
{{> post}}
{{/has}}
{{/foreach}}
You could add this code to a home.hbs
file and it would only be used on your homepage.
I'm not sure of the best way to limit it to one post if you want other list pages to have more than one post though. You may have to write a custom helper.
You do have access to an @index
variable, but if the first post with 'news' is the third post, @index
will be 2
because it increments with the outer foreach
loop.
Soon you should be able to use the api: https://github.com/TryGhost/Ghost/wiki/%5BWIP%5D-API-Documentation
After having a read of the lengthy discussion GitHub Ghost Issue: Query (get) helper #4439 recently closed, great news - helpers and filters are being added to Public API v1!
The {{#get}} helper #5619 has just been merged to master (still unstable), so the solution:
{{#get "posts" featured="true" as |featured|}}
{{#foreach featured}}
...
{{/foreach}}
{{/get}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With