How can I exclude specific tags from single post?

Hay, I am trying to exclude some specific tags from current single post tags and I want to show rest of tags of that post. I am trying in the following way but show all the tags of the site.

{{#get "tags" filter="slug:-[nevy, nova, pink, violet]"}}
    {{#foreach tags}}
            <li class="breadcrumb-item"><a href="{{url}}">{{name}}</a></li>
    {{/foreach}}
{{/get}}

How can I solve this?

Thanks

The get helper makes a fresh API request to fetch data, nothing to do with the current single post, so {{#get "tags"}} returning all tags except those you filtered out is the expected behaviour.

If you want to loop over the tags associated with the current post, you would only need to do {{#foreach tags}}{{/foreach}}.

If you want to ignore some, you can use the negated form of the {{^has}}{{/has}} has helper, and match on the slug: Ghost Handlebars Theme Helpers: has

There’s not a super nice way to do filter multiple tags at once like this at the moment but it can be done by nesting the has helper…

// remove the get helper
{{#foreach tags}}
{{^has slug="nevy"}}{{^has slug="nova"}}{{^has slug="pink"}}{{^has slug="violet"}}
   <li class="breadcrumb-item"><a href="{{url}}">{{name}}</a></li>
{{/has}}{{/has}}{{/has}}{{/has}}
{{/foreach}}

An alternative, more standard approach to this is to use internal tags for the tags you don’t want to appear on the frontend, as these will be filtered from the foreach loop automatically.

1 Like