Count for Tagged Items in a Collection


Hi All,

I decided to redo the structure of my site and move some of my Posts to Collections - to be more semantic and friendly. I had a couple an archive page set up that would show posts grouped by Tags. At the top was the list of tags with a post count badge, and below was a list of all the posts under those headings. I’ve managed to recreate the tag lists but I can’t seem to get a collection item count.

The old code used {{ site.tags[tag].size }} - so I’m wondering if there is an equivalent for getting the same information for tags used in collections?



Some more information:

{% assign collection_tags = | map: 'tags' | join: ','  | split: ',' | uniq | sort %}

    <ul class="tags">
        {% for tag in collection_tags %}
            <li><a href="#{{ tag | slugify: 'pretty' }}" class="tag">{{ tag }}<span>({{ site.tags[tag].size }})</span></a></li>
        {% endfor %}


My memory is hazy on this, but when I tried to do something similar awhile back I don’t think it was possible, though things may have changed.

At the time site.tags and site.categories were only applied to posts and not available to all collection documents.


Thanks for the response. That seems to be the case. Can you recommend an alternative way of getting that value? I seem to be a little stuck on this one detail. Is there a way to query how many items each of the tags found in collection_tags?


I found the following on StackOverflow -

It seems to do the job but is a bit verbose. Seems to be OK as a work around but would love there to be a simpler way.