Get Certain Tags


#1

Hello - I would like to create a widget/side menu that displays a list of tags, but I only want certain tags that I plan to use a categories.

First off this is my second day using ghost and trying to learn handlebars etc… So I’m drowning or barley keeping a float. So far I love what Ghost has to offer.

  1. My first idea is to use a pattern where all stories the first tag/primary tag would be the category and display those so it’s dynamic in a sense.
  2. My second Idea is to simply hard code the tags using conditional logic I want as I’m looping through them.

How do I go about doing this. Any help is appreciated.

Thanks


#2

Hi there,

Have you had a look at the get helper? It’s designed for exactly this kind of thing. You can use a filter to fetch all tags in a group, like this

{{#get "tags" filter="slug:[tag-1, tag-2, tag-3]"}}{{/get}}

You’d combine this with a foreach loop, and some markup, something like this:

{{#get "tags" filter="slug:[tag-1, tag-2, tag-3]"}}
  {{#foreach tags}}
    <a href="{{url}}">{{name}}</a>
  {{/foreach}}
{{/get}}

This gives you your option 2 - a hard coded list.

Currently it’s not possible in a performant/sensible way to make a request for all the tags that are used as primary tags, but we’re working on advanced blog structures like this, so I’d highly recommend using the hardcoded version for now, and updating your code later when we have the ability to do what you need more easily.


How to display custom tags
#3

Hi - Thank you for the response.

Yes - I finally came across the ghost api docs and landed up doing something similar.

{{#get "tags" filter="name:tag-1,name:tag-2,name:tag-3"}}
  {{#foreach tags}}
    <a href="{{url}}">{{name}}</a>
  {{/foreach}}
{{/get}}

Thank you for letting me know about not being able to get primary tags in a performant/sensible way. I still wanted to tackle that. :slight_smile: