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.

This solution has been tested in a GitHub-hosted website:

I use Jekyll on my GitHub-hosted page, you can use group_by to achieve so:

{% assign alldocs = site.COLLECTIONNAME | <additional filtering and sorting> %}	
{% assign grouptag =  alldocs | map: 'tags' | join: ','  | split: ','  | group_by: tag %}
{%- for tag in grouptag -%}
    <h1>{{- -}} - {{tag.size}}</h1>
	{%- for document in alldocs -%}
    	{% if document.tags contains %}
        	 <p>{{- document.title -}}
		 {% endif %}
    {%- endfor -%}
{%- endfor -%}

Live example of a tag cloud including all the content of my blog (posts and projects) here

@dieghernan Sorry to say your link is “404”…

Thank! Should be Tags | One world