1

I have a set of FAQ entries. Each entry has one or more categories attached. To be able to filter entries related to a category I need a list with unique categories.

Using my solution I'm getting all (some double) categories.

   {% set entries = craft.entries.section('faqs') %}
   {% for entry in entries %}
        {% for category in entry.faqCategory %}
             <li><a href="#{{ category.title }}" class="filter" data-filter=".{{ category.title }}">{{ category.title }}<ins>&nbsp;</ins></a></li>
        {% endfor %}
    {% endfor %}
1

Maybe not the most elegant, but something like this should work:

{% set entries = craft.entries.section('faqs') %}
{% set usedCategoryIds = [] %}

{% for entry in entries %}
    {% for category in entry.faqCategory %}
        {% if category.id not in usedCategoryIds %}
            <li><a href="#{{ category.title }}" class="filter" data-filter=".{{ category.title }}">{{ category.title }}<ins>&nbsp;</ins></a></li>

            {% set usedCategoryIds = usedCategoryIds|merge([category.id]) %}
        {% endif %}
    {% endfor %}
{% endfor %}
  • And it does! That solved the issue. – Robbie Post Apr 19 '17 at 6:25

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.