I’m having trouble setting up an additional category listing page from the one I have set with the Category URI Format and Template settings in my Category Site Settings. Specifically, I’m having trouble getting it to work on my site which has two locales.
I have one category group called “practices” and one structure section-type called “people.” I’m trying to create two different pages that each loops through all the entries for a particular category.
In other words, domain.com/people/practices/categoryA and domain.com/people/foo/practices/categoryA would both display all the “people” entries that belong to categoryA.
For my “practices” category group, I have the Category URIs to be people/practices/{slug} and the template to be people/_practices. Everything on this category listing page at domain.com/people/practices/categoryA works as intended.
I can also get the category listing page at domain.com/people/foo/practices/categoryA to work by using the last segment (which would be the category slug) and setting the category object by passing in the category slug like this:
{% set categorySlug = craft.app.request.segments|last %}
{% set category = craft.categories.slug(categorySlug).one() %}
However, this category listing page is throwing an error when I’m using it with my language switcher code, which uses:
{% if category is defined %}
{% set entry = category %}
{% endif %}
{% set supportedSites = entry.getSupportedSites %}
<ul>
{% for siteForEntry in supportedSites %}
{% if siteForEntry | length == 1 %}
{# if category #}
{% set site = craft.app.getSites.getSiteById(siteForEntry) %}
{% set entryForOtherSite = craft.categories.id(entry.id).site(site).one() %}
{% elseif siteForEntry | length == 2 %}
{# if standard entry #}
{% set site = craft.app.getSites.getSiteById(siteForEntry.siteId) %}
{% set entryForOtherSite = craft.entries.id(entry.id).site(site).one() %}
{% endif %}
{% if site.id == entry.siteId %}
{# This is the current language, mark it as active and don't retrieve its url #}
<li class="current {{ site.language }}">{{site.name}}</li>
{% else %}
{# if it's not the current site, get the entry for this site #}
<li class="{{ site.language }}"><a href="{{ entryForOtherSite.getUrl() }}">{{site.name}}</a</li>
{% endif %}
{% endfor %}
</ul>
Specifically, I get the “Variable ‘entry’ does not exist” error. I’m not sure why I’m getting this error because I’m setting the “entry” variable with the “category” variable. This works with domain.com/people/practices/categoryA, but it’s not working for the other category listing page.
{% if category is defined %}
{% set entry = category %}
{{ entry.id }}
{% endif %}
Not sure what to do. I also set a route so that domain.com/people/foo/practices/{slug} loads the people/_practices template, but it still doesn’t work.