Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

handlebars.js - 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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...