2

I've got a for loop working perfect with a limit of 5. Then I throw in the if statement to filter those entries that correspond with a global variable I have set. The problem is the if statement is after the for loop with the limit and only doing it's thing to the 5 entries and not all of the entries.\

I want to use the if statement on all the entries to return the ones I want, then limit it to 5. In another DIV I have and offset(5), so the rest go there. I hope that makes sense.

{% for entry in craft.entries.section('news').order('articleOrder desc').limit(5).find() %}

    {% if entry.currentYear2 == currentEdition.currentYear2 and currentEdition.currentMonth2 in entry.currentMonth2 %}

        <h2>{{ entry.title }}</h2>

        <p>{{ entry.body }}</p>

    {% endif %}

{% endfor %}

1 Answer 1

1

If I'm following you correct, you can use a conditional in a for loop to do what you want.

So in your case, it would be something like:

{% for entry in craft.entries.section('news').order('articleOrder desc').limit(5).find() if entry.currentYear2 == currentEdition.currentYear2 and currentEdition.currentMonth2 in entry.currentMonth2 %}
   ...
{% endfor %}
2
  • It's not doing anything different. I have 10 entries total. 6 meet the if statement criteria. When the limit is set at 20, 6 entries are returned. When the limit is set at 5, only 1 entry is returned when I want it to return 5.
    – afc
    Commented Mar 23, 2017 at 13:16
  • I think the answer you are looking for is in Brads answer here -> craftcms.stackexchange.com/questions/21046/… That takes into account the amount of times a valid entry has been found.... Commented Mar 29, 2018 at 14:18

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.