How to find all messages with a specific tag in Ghost and iterate over them?

I am currently working on a Ghost blog (Ghost is the successor to Wordpress), which is based on Node.js and other various packages / libraries on this platform), but I am wondering how I can be to capture all posts with a specific tag in Ghost / Handlebars.js.

The problem is that Ghost contexts are usually encapsulated to such an extent that I cannot retrieve a list of all messages with a specific tag from the API; apparently, it is only possible to index.hbs over messages from index.hbs , and other solutions - a little hacker-y or use more jQuery.

How can I get a list or array of all messages in Ghost so that I can filter them by tag and then iterate over them? I even tried {{#foreach posts}} and {{#has tag='WHATEVER'}} , but this method does not seem to work out of the box. As a newbie to Ghost and Handlebars, I'm not sure what to do.

+6
source share
2 answers

In case someone meets all this, now it is possible. Here's how you can do it through the get helper:

 {{#get "posts" filter="tags:tagname"}} {{#foreach posts}} <p>{{title}}</p> {{/foreach}} {{/get}} {{#get "posts" filter="tags:[tag1, tag2]"}} {{#foreach posts}} <p>{{title}}</p> {{/foreach}} {{/get}} 
+10
source

Note. This answer was correct at the time of writing. The {{#get}} was added in November 2015 and is available by default with Ghost 1.0 (Aug 2017). This is described here: https://themes.ghost.org/docs/get

David's answer should now be an accepted answer.


A list of all tags is currently not possible, as described in the FAQ . It also refers to getting an auxiliary function in the roadmap that will make this possible in the future.

One of the few hacked features with the current version of Ghost is to use JavaScript to retrieve RSS feed pages and scroll through each page that captures tags from each post. This will work, but it is worth keeping in mind that the RSS feed pagination will disappear in a future version (after the API is fully accessible, therefore there will be a migration path).

Once the helper is released, get will become a simple helper: {{#get 'tags'}}...do things with tags here...{{/get}} . This feature is under active development.

+6
source

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


All Articles