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?

Thanks!

Some more information:

{% assign collection_tags =  site.my-collection | 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 %}
    </ul>

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 - http://stackoverflow.com/questions/36479756/counting-collection-tags-in-jekyll

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.name -}} - {{tag.size}}</h1>
	{%- for document in alldocs -%}
    	{% if document.tags contains tag.name %}
        	 <p>{{- document.title -}}
		 {% endif %}
    {%- endfor -%}
{%- endfor -%}

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

1 Like

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

Thank! Should be Tags | One world

1 Like

Sorry to say this not work any more on Jekyll >= 4.1.0. I implemented a solution in pure liquid that should work in any version (link):


{% assign alldocs = site.documents %}
{% assign alltags = alldocs | map: 'tags' | join: ',' | split: ',' %}
{% assign single_tags = alltags | uniq %}

<!-- Counting -->
{% assign count_tags = '' | split: ',' %}
{% assign n_tags = single_tags | size | minus: 1 %}
{% for i in (0..n_tags) %}
  {% assign count_this_tag = alltags | where_exp:"item", "item == single_tags[i]" | size %}
  {% assign count_tags = count_tags | push: count_this_tag %}
{% endfor %}

<!-- Extra: sort -->
{% assign items_max = count_tags | sort | last %}
{% assign sorted_tags = '' | split: ',' %}
{% assign sorted_count_tags = '' | split: ',' %}

{% for i in (1..items_max) reversed %}
  {% for j in (0..n_tags) %}
    {% if count_tags[j] == i %}
     {% assign sorted_tags = sorted_tags | push: single_tags[j] %}
     {% assign sorted_count_tags = sorted_count_tags | push: i %}
    {% endif %}
  {% endfor %}
{% endfor %}

{% assign sorted_tags = sorted_tags | uniq %}

 {%- for i in (0..n_tags) %}
      <p>{{ sorted_tags[i] }} - {{ sorted_count_tags[i] }} </p>
  {%- endfor -%}