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.