2

I have a category page that lists all of the entries related to that category. And then I want to display the selected child category for the entry... in brief like so:

Entry Title 1
Entry 1's Child-Category

Entry Title 2
Entry 2's Child-Category

Entry Title 3
Entry 3's Child-Category

My category field handle is chooseLocation

Here is what I have in my template: (snippet taken from here)

{% for entry in entries %}
    {% if entry.chooseLocation | length %}
    {% for category in entry.chooseLocation %}
         <a href="/location/{{ category.slug }}">{{ category }}</a>
            {% if not loop.last %}, {% endif %}
        {% endfor %}
    {% endif %}
{% endfor %}

The problem is, this get's the parent category (which is the category page we're on) but not the child category. I need to show just the child category and not the parent.

I've looked through the docs but I can't find anything on how to do this.

3

Your Categories field (entry.chooseLocation) is an Element Criteria Model. It's exactly the same as if you'd done something like this...

craft.categories.group('myGroup')

The Element Criteria Model (ECM) is an important concept in Craft, because it's the driver for most content handling. Once you've got a bearing on what the ECM is, you can use it fairly consistently.

For example, just like craft.entries, you can apply .find, .first, and other methods to retrieve a specific subset of elements. And depending on the element type, you can chain other filtering methods in between...

craft.categories.group('myGroup').limit(3).find()

Returning to your question, entry.chooseLocation will be an ECM for Categories. So it's just a matter of filtering it appropriately.

{% for category in entry.chooseLocation.level(2).find() %}

Here is the full list of filtering options you can use on a Categories ECM.

  • Hmmm I may also have something else buggy going on... When I update my code to include .level(2).find() like you show above, I get nothing, when I change it to .level(1).find() it works and shows the parent. I double checked my entries I'm testing against, and I've selected the child category and saved, but when I look at the chooseLocation field, it shows the parent category as the chosen option. Screenshots - select the child category: goo.gl/RDjCzj and what shows after selecting the child category: goo.gl/HRJssb. I'm not sure if that is normal behavior or not though. – ByteMyPixel Sep 22 '16 at 19:26
  • Ah, I think I see what's happening... When you created the field, did you limit the maximum to 1? That actually prevents child categories from being selected (since the parent will always be selected). – Lindsey D Sep 22 '16 at 19:29
  • 1
    Yes, that was it! I had the limit set to 1 :/ I removed that, reselected the child category for each entry, and now {% for category in entry.chooseLocation.level(2).find() %} works perfectly :) – ByteMyPixel Sep 22 '16 at 19:35
  • Great! It's a quirky limitation, and ends up being a "gotcha" for a lot of people. :) – Lindsey D Sep 22 '16 at 19:36

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.