<p><code>{% for entry in craft.entries.section('sponsors').showOnHome('1') %}</code></p>

<p>The <code>.showOnHome('1')</code> restricts your query to ones with the lightswitch on.</p>

<p>If you wanted only the ones with the lightswitch off, you would use <code>.showOnHome('not 1')</code></p>

CC BY-SA 3.0
2014-07-03T18:02:24.523Z
Thank you - I thought that was the first thing I tried, but works great!
Andrew
CC BY-SA 3.0
2014-07-03T17:59:40.467Z
2014-07-03T17:59:40.467Z
Marion Newlevant
<p>While Marion’s answer is <strong>definitely</strong> the right way to go about this, since you’re new with Twig I’ll show you how you could also do this with a Twig conditional.</p>

<p>You could use an <a href="http://twig.sensiolabs.org/doc/tags/if.html" rel="nofollow"><code>{% if %}</code> tag</a> to check if your showOnHome field is enabled.</p>

<pre><code>&lt;ul&gt;
    {% for entry in craft.entries.section('sponsors') %}

        {% if entry.showOnHome %}

            {% for image in entry.sponsorlogo %}
                &lt;li&gt;&lt;a href="{{ entry.getUrl }}"&gt;&lt;img src="{{ image.getUrl() }}" alt="{{ image.title }}"&lt;/a&gt;&lt;/li&gt;
            {% endfor %}

        {% endif %}

    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Hope that helps. Again, using the <code>showOnHome(1)</code> parameter on <code>craft.entries</code> query is a much better way to go, since you will be saving Craft the trouble of fetching those entries to begin with.</p>

CC BY-SA 3.0
2014-07-03T18:14:49.813Z
2014-07-03T18:14:49.813Z
Brandon Kelly
<p>I'm pretty new to Twig and I know the level here is far above my question, but thank you in advance...</p>

<p>I'm designing a site with sponsor logos on the homepage, each sponsor is an entry in a channel of sponsors. I have a lightswitch field on the entry page called 'showOnHome'.  I've tried "if" statements and conditionals in the loop, but have only got to where it shows the first entry and breaks the loop.</p>

<p>The following works to get all of the entries, </p>

<pre><code>&lt;ul&gt;
    {% for entry in craft.entries.section('sponsors') %}
        {% for image in entry.sponsorlogo %}

            &lt;li&gt;&lt;a href="{{ entry.getUrl }}"&gt;&lt;img src="{{ image.getUrl() }}" alt="{{ image.title }}"&lt;/a&gt;&lt;/li&gt;

        {% endfor %}
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>How do I only get entries where showOnHome == '1'?</p>

<p>Thank you in advance for helping the rookie.</p>

CC BY-SA 3.0
2014-07-04T05:17:42.393Z
I think I can speak for everyone and say that the level here is at any level you need. Everyone is very happy to help and teach.
Anna_MediaGirl
<templating>
Looping Entries Based on Field State
# Looping Entries Based on Field State

(Asked by: username_0 on 2014-07-03)

I'm pretty new to Twig and I know the level here is far above my question, but thank you in advance...
I'm designing a site with sponsor logos on the homepage, each sponsor is an entry in a channel of sponsors. I have a lightswitch field on the entry page called 'showOnHome'.  I've tried "if" statements and conditionals in the loop, but have only got to where it shows the first entry and breaks the loop.
The following works to get all of the entries, 

    {% for entry in craft.entries.section('sponsors') %}
        {% for image in entry.sponsorlogo %}

            

        {% endfor %}
    {% endfor %}


How do I only get entries where showOnHome == '1'?
Thank you in advance for helping the rookie.

**Tags:** templating

> **Comments:**
> **username_1**: I think I can speak for everyone and say that the level here is at any level you need. Everyone is very happy to help and teach.  

---

## 2 answers

### Answer by username_2 on 2014-07-03. Score: 8

`{% for entry in craft.entries.section('sponsors').showOnHome('1') %}`

The `.showOnHome('1')` restricts your query to ones with the lightswitch on.

If you wanted only the ones with the lightswitch off, you would use `.showOnHome('not 1')`

> **Comments:**
> **username_0**: Thank you - I thought that was the first thing I tried, but works great!  

---

### Answer by username_3 on 2014-07-03. Score: 3

While Marion’s answer is definitely the right way to go about this, since you’re new with Twig I’ll show you how you could also do this with a Twig conditional.
You could use an {% if %} tag to check if your showOnHome field is enabled.

    {% for entry in craft.entries.section('sponsors') %}

        {% if entry.showOnHome %}

            {% for image in entry.sponsorlogo %}
                
            {% endfor %}

        {% endif %}

    {% endfor %}


Hope that helps. Again, using the showOnHome(1) parameter on craft.entries query is a much better way to go, since you will be saving Craft the trouble of fetching those entries to begin with.


---

