1

I have a long list of categories in a specific category group, but only want to show a list of the categories from this group that currently have an entry associated with them.

I've been trying to implement the solution that I found at this thread: How can I output a list of categories that are not empty?

Only categories with related entries are showing. However, it's not being narrowed down to specific to a specific category group. Here's my code below:

{% set handshapeCategories = craft.categories.group('handshape').limit(null) %}
{% set entriesWithHandshapeCategories = craft.entries.relatedTo( handshapeCategories ).limit(null) %}
{% set handshapeCategoriesInUse = craft.categories.relatedTo(entriesWithHandshapeCategories) %}

{% for handshapeCategory in handshapeCategoriesInUse %}
    <h3>{{ handshapeCategory }}</h3>
{% endfor %}       

Any ideas why the shown categories are not being limited to categories from the 'handshape' category group?

1
  • If you got a solution to your problem, please consider accepting one of the answers below Commented Jul 31, 2015 at 20:02

3 Answers 3

5

I would reverse your logic:

  1. get all entries in whatever channel
  2. display only categories (from a certain group) that have entries fetched in step 1 attached to them

Something along the lines of:

{% set entries = craft.entries.section('myChannel').limit(null) %}
{% set activeCategories = craft.categories.group('myCatgroup').relatedTo(entries).find() %}

You can then use a simple for loop to display only the active categories

2
  • Yeah, this works really well and is easy to understand.
    – Justin K
    Commented Aug 17, 2015 at 17:42
  • Glad it helps. You might also be interested in a more advanced set of tricks I blogged about: webstoemp.com/blog/… Commented Aug 19, 2015 at 7:01
1

In order to limit the related categories to a specific category group I needed to add .group('desiredCategoryGroup') to the last line.

Here's an example of what I came up with:

{% set newsCategories = craft.categories.group('news').limit(null) %}
{% set soriesWithNewsCategory = craft.entries.relatedTo( newsCategories ).limit(null) %}
{% set newsCategoriesInUse = craft.categories.relatedTo(soriesWithNewsCategory).group('news') %}

<h2>List of News Categories in Use:</h2>
<ul>
    {% for category in newsCategoriesInUse %}
        <li>{{ category.title }}</li>
    {% endfor %}
</ul>
1

I don't know how it compares efficiency-wise, but another approach to this (which I personally find much easier to read) would be something like:

<ul>
  {% for handshapeCategory in craft.categories.group('handshape') %}
    {% if craft.entries.relatedTo(handShapeCategory).limit(1).getOne() is not empty %}
      <li>{{ category.title }}</li>
    {% endif %}
  {% endfor %}
</ul>

In other words, for each category in the group, if we can fetch an entry for that category, display the title.

1
  • This is not very effective as it will result in one db query for each category.
    – carlcs
    Commented Jul 20, 2015 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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