2

There is a publishMonth (multiselect) and publishYear (radio buttons) field in each article. They do not correspond to an actual date. Each article is is associated with one year, or one or more months. I'd like my editor to be able to change two global variables. currentMonth (radio button), and currentYear (radio button) to change all the articles display on the webpage at once at any time she pleases. Some articles may be associated with one or more months. Articles do not get changed over at the same date and time each month, which is why it's not associated with a real month or year. The code below returns nothing.

The idea is to make it as easy as possible for my editor to "flick the switch" to the next month.

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

    {% if entry.publishYear in currentEdition.currentYear %}

    <a href="{{ entry.url }}"><h2>{{ entry.title }}</h2></a>
    {% for image in entry.images.limit(1) %}
        <img src="{{ image.getUrl('frontPageThumbnail') }}" class="main-image">
    {% endfor %}
    <p>{{ entry.body|striptags|slice(0,300) }}... <a href="{{ entry.URL }}" class="readmore">Read more</a></p>
    <p>{{ entry.publishYear }}</p>
    <ul>
        {% for option in entry.publishMonth %}
            <li>{{ option }}</li>
        {% endfor %}
    </ul>

    {% endif %}

{% endfor %}
  • The for loop returns everything as per normal without the if statement I threw in there. – afc Mar 15 '17 at 17:04
1

Since you've confirmed that the issue is in the if statement and I can see your comparison, I'll clarify usage and propose a solution.

The in operator, is a containment operator and it expects a scalar|primitive value on the left and an array|iterable on the right.

I am assuming that entry.publishYear is an int or a string: 2017. However, I'm not sure about currentEdition.currentYear. It seems like it's also an int or string: 2017.

If the currentEdition.currentYear is not an array or an iterable item, you should use == instead of in.

{% if entry.publishYear == currentEdition.currentYear %}

Have you tried that? Or can you try it and let me know?

  • I have tried that. meant to use that in my question. I tried "matches" == and some others. "Same as" maybe. all return nothing. The values are all strings. I have not tried them as integers. I could. It would involve my editor inputting the numbers that represent the month and year instead of selecting them with a click of the mouse. A minor thing. – afc Mar 15 '17 at 17:28
  • And you've checked that some of the comparisons actually match or should match? – Selvin Ortiz Mar 15 '17 at 17:35
  • I've check the variables outside the for loop, and inside it, without the if statement and they both return properly. Perhaps there is a space in a variable somewhere. – afc Mar 15 '17 at 18:29
  • Got it working with {% if entry.currentYear2 == currentEdition.currentYear2 and currentEdition.currentMonth2 in entry.currentMonth2 %}, but all the variables had to be INT and not String. – afc Mar 19 '17 at 15:32

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.