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)
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:
In
index.hbs
:In
tag-videos.hbs
:Hope this helps!
You can definitely filter posts by tag using the
{{has}}
helper: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 be2
because it increments with the outerforeach
loop.Soon you should be able to use the api: https://github.com/TryGhost/Ghost/wiki/%5BWIP%5D-API-Documentation
I've created a little hack for Ghost. It adds {{by_tag}} helper to the theme templates
require('./helpers')();
{{#by_tag}} Helper
Select posts by tag. Optional
limit
parameter.Example:
{{#node_env}} Helper
Example:
Just to add a little useful info, if you want to add featured posts, this is how:
To get a rough idea of what more you can do, visit the official GitHub
Hope it helps