Ghost: new entry with a specific tag on the first page

I am developing a Ghost template for my blog. And I want to see on the first page only one new message from messages with a specific tag (for example, "news").

Is it possible to filter messages by tag in a foreach ?

Any other solution? (the first thing that comes to mind is the trick with the custom tag-news.hbs and rewriting the url)

+5
source share
4 answers

You can definitely filter messages by tags using the {{has}} helper:

 {{#foreach posts}} {{#has tag="news"}} {{> post}} {{/has}} {{/foreach}} 

You can add this code to your home.hbs file and it will only be used on your home page.

I'm not sure how best to limit it to one post if you want more than one post on other pages of the list. You may need to write a custom helper.

You have access to the @index variable, but if the first message with 'news' is the third message, @index will be 2 because it grows with the outer foreach .

Soon you will be able to use the api: https://github.com/TryGhost/Ghost/wiki/%5BWIP%5D-API-Documentation

+11
source

I created a small hack for the Ghost. He adds {{by_tag}} helper to theme templates.

  • Create helpers.js in the ghost directory with the code from this value
  • Add the first line to your config.js: require('./helpers')();
  • Restart ghost

{{# by_tag}} Helper

Select posts by tag. Optional parameter limit .

Example:

  {{#by_tag 'dev'}} {{#foreach posts}} {{title}} {{content}} {{/foreach}} {{/by_tag}} {{#by_tag 'music' limit=3}} {{#foreach posts}} {{title}} {{content}} {{/foreach}} {{/by_tag}} 

{{# node_env}} Helper

Example:

 {{#node_env production}} ...production only {{/node_env}} 
+5
source

After a long discussion of GitHub Ghost Issue: Query (get) helper # 4439 recently closed, great news - helpers and filters are added to the Open API v1 !

Assistant {{#get}} # 5619 has just been merged with the master (still unstable), so the solution is:

 {{#get "posts" featured="true" as |featured|}} {{#foreach featured}} ... {{/foreach}} {{/get}} 
+3
source

In index.hbs :

  <div class="post-feed"> {{#foreach posts}} {{^has tag="videos"}} {{! this block will not show posts tagged videos }} {{> "post-card"}} {{/has}} {{/foreach}} </div> 

In tag-videos.hbs :

  <div class="post-feed"> {{#foreach posts}} {{#has tag="videos"}} {{! this block will show posts tagged videos }} {{> "post-card"}} {{/has}} {{/foreach}} </div> 

Hope this helps!

0
source

Source: https://habr.com/ru/post/1208570/


All Articles