1

I have a set of categories that I would like to display on a sidebar as a way to filter the list of entries displayed. However, some of those categories haven't been associated with any entries yet so clicking on that category with return 0 results. What is the most efficient way to exclude those categories?

1

You could get the entries ids, then get the categories related to those entries. Like so:

{%- set ids= craft.entries.section('sectionHandle').ids() -%}

{%- for cat in craft.categories.relatedTo({ sourceElement: ids }).group('categoryHandle') -%}
    {{- cat.title -}}
{%- endfor -%}

That will only print categories with an entry associated with.

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.