Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ghost: newest post with specific tag on the front page

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)

like image 512
ofstudio Avatar asked Dec 08 '14 11:12

ofstudio


2 Answers

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

like image 54
cjspurgeon Avatar answered Jan 04 '23 00:01

cjspurgeon


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}}
like image 33
Leo Avatar answered Jan 04 '23 00:01

Leo