1

I have two category groups - brands and resources. Under these I have product videos and documentation entries - section ‘resources’. I’m using the brands categories elsewhere for products too so I’ve taken care to use .section(‘resources').

I have three templates - a landing page (all good), a category page (all good) which shows the resources categories entries and filtered by the resources categories fine. This is the URL structure

http://kelairpumps-craft.local/resources/product-videos

enter image description here

I have a further ‘brands’ template which I’m trying to combine the two categories for - URL structure:

http://kelairpumps-craft.local/resources/product-videos/atlas

I’ve succeeded in filtering the entries fine but cant for some reason get the ‘brands’ nav to work properly. Whatever I do it's showing either nothing under brands or all of the brands categories with entries when I need it to show for example product-videos + brand.

The problem I have is that it is showing the Corken brand category when the product-videos and a brand category are selected which it shouldn’t because there are no product videos for Corken. I’ve only added Corken under Operating manuals.

enter image description here

enter image description hereenter image description here I created this route:- I’m just not grokking the source/target/element part of the relatedTo equation.

The ‘brands’ template follows which is show for the third segment situation. Please tell me what I’m doing wrong here…

{# get categories from url segments #}
{% set resourceSlug = craft.app.request.segment(2) %}
{% set brandSlug = craft.app.request.segment(3) %}

{# match current categories #}
{% set currentCategory1 = craft.categories.group('resources').slug(resourceSlug).one() %}
{% set currentCategory2 = craft.categories.group('brands').slug(brandSlug).one() %}


<div class="body-copy">

    <header class="inset">
        <h1>{{ currentCategory1 }} - {{ currentCategory2 }}</h1>
    </header>

    {% paginate craft.entries.relatedTo(['and',
        { targetElement: currentCategory1.id, field: 'resourceCategories' },
        { targetElement: currentCategory2.id, field: 'brands' }
    ]).limit('12').orderBy('title') as pageInfo, catentries %}

    <ul class="hlisting">
        {% for entry in catentries %}
            <li class="entry clearfix">

                {% set teaserimage = entry.resourceImage.one() %}
                {% if teaserimage %}
                    <div class="img-container vid-thumb">
                        <a class="image" href="{{ entry.url }}">
                            <img class="border" alt="{{ teaserimage.title }}" srcset="{{ teaserimage.getUrl('smallListingThumb') }}" src="">
                        </a>
                    </div>
                {% endif %}

                <h2 class="">
                    <a href="{{ entry.url }}">{{ entry.title }}</a>
                </h2>

                {{ entry.resourceSummary }}

            </li>
        {% endfor %}
    </ul>

    {% if pageInfo.totalPages > 1 %}
        {% include '_includes/pagination' %}
    {% endif %}

</div>


<aside class="secondary-copy">
    <div class="gutter">

        <h2 class="h4"><a href="/resources">Resources</a></h2>

        {# Resource Categories Global Query #}
        {# Grab all entry ID's from Resources #}
        {% set entryIds = craft.entries.section('resources').ids() %}

        {# Create a category query with the resources 'group' parameter #}
        {% set myCategoryQuery = craft.categories().group('resources') %}

        {# match current category for active class #}
        {% set categorySlug = craft.app.request.segment(2) %}
        {% set currentCategory = craft.categories.group('resources').slug(categorySlug).one() %}

        {# Fetch the categories - only return categories that have a relationship with one of our entry ids #}
        {% set categories = myCategoryQuery.relatedTo({ sourceElement: entryIds }).all() %}

        {# Create a category query with the brands 'group' parameter #}
        {% set myBrandCategoryQuery = craft.categories().group('brands') %}

        {# match current brands category for active class #}
        {% set brandsCategorySlug = craft.app.request.segment(3) %}
        {% set currentBrandCategory = craft.categories.group('brands').slug(brandsCategorySlug).one() %}

        {# Fetch the categories - only return categories that have a relationship with one of our entry ids #}
        {% set brandCategories = myBrandCategoryQuery.relatedTo({ sourceElement: entryIds }).all() %}

        {# Display the navigation #}
        <ul class="section_nav">
            {% nav category in categories %}
                {% set activeClass = category.id == currentCategory.id ? 'selected' %}
                <li{% if activeClass %} class="{{ activeClass }}"{% endif %}>
                    <a href="{{ category.url }}">{{ category.title }}</a>
                </li>
            {% endnav %}
        </ul>

        {# Display the brand navigation #}
        <h2 class="h4">
            <a href="/brands">By Brand</a>
        </h2>

        <ul class="section_nav">
            {% nav category in brandCategories %}
                {% set activeClass = category.id == currentBrandCategory.id ? 'selected' %}
                <li{% if activeClass %} class="{{ activeClass }}"{% endif %}>
                    <a href="{{ siteUrl ~ 'resources/'  ~ categorySlug ~ '/' ~ category.slug }}">{{ category.title }}</a>
                </li>
            {% endnav %}
        </ul>

    </div>
</aside>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.