Display all categories in a Blog page filtered

Hi.
I’m trying to do a partial template to show all blog categories in the sidebar.
I use this code for this porpose:

{# Set Categories #}
		{% set terms = [] %}
		{% for child in collection %}
			{% for term in  child.taxonomy.category %}
				{% if term not in terms %}
					{% set terms = terms|merge([term]) %}
				{% endif %}
			{% endfor %}
		{% endfor %}

		{# Set pages number by category #}
		{% set catItemsCounts = [] %}
		{% for item in blog.children %}
			{% for cat in item.taxonomy.category %}
				{% set catItemsCounts = catItemsCounts|merge({(cat): (catItemsCounts[cat]|default(0)) + 1}) %}
			{% endfor %}
		{% endfor %}

The next code is where I call these categories in this way:

{% for term in terms %}

        <div class="col-4 py-2">
            {% for p in taxonomy.findTaxonomy({'category' : term}).order('date', 'desc').random(1) %}
            {% set blog_image = p.media.images[p.header.featuredImage] ?: p.media.images|filter((v, k) => k != p.header.avatarImage)|first %}
                {{ blog_image.cropZoom(100,60).loading('lazy').attribute('decoding','async').html('Posts in {{term|raw}}', 'Posts in {{term|raw}}', 'mr-2 rounded')|raw }}
            {% endfor %}
        </div>
        <div class="col-8">
            <span class="font-weight-bold text-uppercase">
            <a href="{{blog.url ~ '/category:' ~ term }}" class="text-{{ color_style }}"> {{ term|raw }}</a>
            </span>
            <small class="d-block text-muted">
                <a href="{{blog.url ~ '/category:' ~ term }}">
			<div class="text-muted mt-1">{{catItemsCounts[term]}}
						{{ 'MYTHEME.SIDEBAR.CATEGORIES.POSTS_IN'|t }}
						{{term|capitalize|raw}}</div>
		</a>
            </small>
        </div>

{% endfor %}

This code is in the sidebar template and at start of file I have declared the blog page:

{% set blog_theme = theme_var('blog_route')|defined('/blog') %}
{% set blog = page.find(header_var('blog_url')|defined(blog_theme)) %}
{% set new_base_url = blog.url == '/' ? '' : blog.url %}

Well, the problem is when the blog page is loaded without taxonomy filters, for example, https://mysite.com/blog all categories is displayed in sidebar, but when the blog page is loaded with some taxonomy filter, like https://mysite.com/blog:grav, then not shows all categories, only one is displayed in sidebar.

What happen here, and how can I fix this issue?
Thanks so much in advance.

If your blog folder structure is like

blog/category-1/post
blog/category-2/post
blog/category-3/post

u can create nav element based on folder instead of using taxonomies :

{% for p in pages.find('/blog').children.order('folder') %}

Hi @domena.online
First of all, thanks for your answer.

My blog is not structured under the categories folders. It’s a simple structure with blog parent folder and children folders for items, like this:

blog/item1/item.md
blog/item2/item.md

The code I sent before, works fine when you are in the blog main url (/blog) or some item url. The problem come with you click in the category link, and blog is filtered with that category (ej. https://mysite.com/blog:grav). I don’t know if I have explained correctly.

Finally, I’ve found a possible solution with some minors changes in the my code:

{# Set Categories #}
		{% set terms = [] %}
		{% for item in blog.children %}
			{% for term in  item.taxonomy.category %}
				{% if term not in terms %}
					{% set terms = terms|merge([term]) %}
				{% endif %}
			{% endfor %}
		{% endfor %}

		{# Set pages number by category #}
		{% set catItemsCounts = [] %}
		{% for item in blog.children %}
			{% for cat in item.taxonomy.category %}
				{% set catItemsCounts = catItemsCounts|merge({(cat): (catItemsCounts[cat]|default(0)) + 1}) %}
			{% endfor %}
		{% endfor %}

I changed {% for child in collection %} to {% for item in blog.children %} and {% for term in child.taxonomy.category %} to {% for term in item.taxonomy.category %}.

At the moment this code it works fine.