1

I am trying to set an if statment that will show a li if there is only one entry displaying. The issue I am having is that this entry is put into two different places via checking if it contains a checkbox. I can only seem to managed to get it to display all the time or not at all

The code I have at the moment is this {% if entry.ENTRYNAME.contains('CHECKBOX OPTION') == 0 %} <li>CONTENT</li> {% endif %}

But that does not seem to work. I am using the Check box contains option else where and it works fine. It's just the value check that I cannot seem to get to work. I've also tried adding both .length and |length but neither of those seem to work.

2
  • I'm confused on a few things... what does "one entry displaying" mean? What is ENTRYNAME in your example? Is that an Entries field (buildwithcraft.com/docs/entries-fields)?
    – Brad Bell
    Jul 15, 2015 at 3:31
  • @BradBell I made a mistake. ENTRYNAME Is actually FIELDNAME which is a checkbox field. I figured out how to solve the issue and have posted the answer bellow
    – JeffTalbot
    Jul 15, 2015 at 3:37

2 Answers 2

2

OKay so the solution I found is this. It was much more complicated than I wanted it to be but it works.

{% set VARIABLE = 0 %}
{% for entry in craft.entries.section('ENTRY') %}
    {% if entry.FIELDNAME.contains('CHECKBOX OPTION') %}
        {% set VARIABLE = VARIABLE + 1 %}
        <li>CONTENT</li>
    {% endif %}
{% endfor %}
{% if VARIABLE <= 1 %}
    <li>CONTENT</li>
{% endif %}
2

I'm not entirely sure what you're trying to do, so I don't know if these suggestions will help at all.

If you want to check if a checkbox field only has one option checked then you can simply use

{% if entry.FIELDNAME|length == 1 %}
    ...
{% endif %}

If you're trying to show it in one location if a certain checkbox is checked, and a different location if a different checkbox is checked, then try...

{# location one (maybe header) #}
{% if entry.FIELDNAME.contains("Use Location 1") %}
    <li>CONTENT</li>
{% endif %}

{# elsewhere, location two (maybe footer) #}
{% if entry.FIELDNAME.contains("Use Location 2") %}
    <li>CONTENT</li>
{% endif %}
1
  • I have pretty much that exact set up for sorting my <li> and it does work but that was not the issue I was struggling with this time. What I needed to do now was check how many entries had option 1 ticked in the checkbox and if that number was below 1 display another <li>. I did figure this out above though. Thanks for the suggstion though
    – JeffTalbot
    Jul 15, 2015 at 5:23

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.