1

I've created two global variables which my content creator is going to use to turn off a bunch of articles and turn on a bunch more which a slight change in the Global. I have a page where there is a limit of 5 of these articles.

{% set entries = craft.entries.section('news').order('articleOrder desc').limit(5).find() %}

{% for entry in entries if entry.currentYear2 == currentEdition.currentYear2 and currentEdition.currentMonth2 in entry.currentMonth2 %}
    <h2>{{ entry.title }}</h2>          
{% endfor %}

The problem with this is that the conditional only gets 5 entries to compare, and not the entire bunch. I want to first run an if statement on all the entries, then let me do a for loop with a .limit(5). I am not sure how to go about this. A conditional combined with a for loop in the same statement did not work either.

1 Answer 1

0

Entirely possible I'm misunderstanding what you're looking for, but I think something like this should do it:

{% set entries = craft.entries.section('news').order('articleOrder desc').find() %}
{% set matchCount = 0 %}

{% for entry in entries %}
    {% if entry.currentYear2 == currentEdition.currentYear2 and currentEdition.currentMonth2 in entry.currentMonth2 and matchCount <= 5 %}
        {% set matchCount = matchCount + 1 %}
        <h2>{{ entry.title }}</h2>
    {% endif %}          
{% endfor %}
2
  • Thank you, that worked perfect, and I would never have figured that out on my own!
    – afc
    Commented Jul 2, 2017 at 16:03
  • I am having the same problem with offset(5) as opposed to limit. Issue is, that I am failing to understand how your fix works, therefor I can't make it happen for offset.
    – afc
    Commented Jul 2, 2017 at 18:24

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.