2

I have an activity calendar (more like a weekly schedule) which filters per day and per category. Because there are so many activities I can't just load them all in at once so I use some sort of Ajax pagination kind of way which grabs the category handle.

The problem is, it will always use the first category that has been selected for the event instead of going through them all.

So everything works fine as long as 1 category is selected.

Here is my code:

{% if craft.request.getParam('cat') == '' %}
     {% set selectedCat = 'startingCategory' %}
{% else %}
     {% set selectedCat = craft.request.getParam('cat') %}
{% endif %}
<div class="row activiteitenRow" id="tegelContent">
     {% for category in craft.categories.group('leeftijdsGroepen') %}
          {% set query = {
                 section : 'sportidEvenementen',
                 order : 'evenementDatum desc',
                 limit : null,
                 relatedTo : category,
           } %}
           {% set entries = craft.entries(query) %}
           {% for entry in entries 
                  if entry.categories[0]|camel == selectedCat
            %}

            {# activity display code here #}

            {% endfor %}
      {% endfor %}
</div>

I think the problem is in the final forloop:

{% for entry in entries if entry.categories[0]|camel == selectedCat %}

I used .first() Before but it was obvious that would only grab the first category but it needs to use an array but, really can't find a way to fix this.

1

After looking for some more options I found the solution.

At first I was looking for a solution on how to check if the handle was inside of the array but, didn't manage to output the entire array as a conditional. I ended up changing the filter to filter on the category id (should have done that in the first place).

Now I'm filtering by ID so I could just use:

{% for entry in entries if selectedCat in entry.categories.ids() %}

I got some help from this answer after I figured out to use ID's instead of handle's

Converting array of entries to an array of ids

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.