Search code examples
node.jshandlebars.jsghost-blog

Ghost post filter by tag name


I'm trying to create a sidebar for my Ghost blog with the tag name and a list of posts with that tag.

I have tried everything and can't get it to work. Here's the closest I have gotten:

.hbs

{{#foreach tags}}
    <h2>{{tag}}</h2>

    {{#foreach posts filter="tags:{{slug}}"}}
       <a href="{{url}}">{{title}}</a>
    {{/foreach}}

{{/foreach}}

Solution

  • Using the #get helper should solve this. Note that I've used primary_tag which stops duplicate posts from happening. If you don't mind that you can change primary_tag to tag:

    {{#get "tags" limit="all"}}
        {{#foreach tags}}
            <h2>{{name}}</h2>
            {{#get "posts" limit="all" filter="primary_tag:{{slug}}"}}
                {{#foreach posts}}
                    <a href="{{url}}">{{title}}</a>
                {{/foreach}}
            {{/get}}
        {{/foreach}}
    {{/get}}
    

    Hope this helps!