Create a list of posts grouped by tag

Hi,

Still relatively new to ghost and have been learning about themes. As an exercise, I am trying to produce a page that lists posts grouped by tags:

tag1

  • post
  • post
  • post

tag2

  • post
  • post
  • post

Here’s the code I’ve been trying to make work:

```
    {{#get "tags" limit="all" include="count.posts" order="count.posts desc"}}
        {{#foreach tags as |alltags|}}
            <a href="{{ url }}">
            <h2>{{ name }} <small>({{ count.posts }})</small> {{alltags.slug}}</h2>
            </a>
            {{#get "posts" limit="3" filter="tag:{{alltags.slug}}" include="tags,authors"}}
                <ul>
                    {{#foreach posts}}
                        <li>{{ title }}</li>
                    {{/foreach}}
            </ul>
            {{/get}}
        {{/foreach}}
    {{/get}}
```

This is the bit that doesn’t seem to be working, inside the second nested #get call
filter=“tag:{{alltags.slug}}”

If I leave that in the nested #get posts call, nothing is returned.

If I take the filter out of that #get posts call, it does print out the first 3 posts but its obviously not filtered. Additionally, the {{ alltags.slug }} does populate inside of the nested foreach, so I’m wondering why it does not seem to be populated inside the #get posts call

Should I expect this to work? Getting all tags, looping through the tags and then getting all posts with the current tag in that first loop?

Hope this is clear and thanks in advance for any guidance :smiley:

Hey Dan,

  1. The block parameters are used inside {{#get}} helper, not {{#foreach}}. It should be:
{{#get "tags" limit="all" include="count.posts" order="count.posts desc" as |alltags|}}
    {{#foreach alltags}}
        ...
    {{/foreach}}
{{/get}}
  1. The filter parameter for the posts query should be tag:{{slug}}, not tag:{{alltags.slug}}.

Our Ease theme does the same thing on its homepage. Here is the file if you want to look at the code.

1 Like

Thank you! I was blind to my typo with the block parameters. I also modified my alltags.slug to just slug and that worked. I will continue to study the docs to shed my obvious misconceptions on how this works.