<p>You could do it this way:</p>

<pre><code>{% set blockLimit = entry.section.isHomepage() ? 1 : null %}

{% for block in memberships.memberships.limit(blockLimit) %}
  &lt;a href="{{ block.urlLocation }}" class="center" target="_blank"&gt;
    {% for asset in block.logo %}
      &lt;img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" /&gt;
    {% endfor %}
  &lt;/a&gt;
{% endfor %}
</code></pre>

<p><code>entry.section</code> returns the <a href="http://buildwithcraft.com/docs/templating/sectionmodel" rel="nofollow">SectionModel</a> for the entry, and <code>isHomepage()</code> tells you whether that section is the homepage. Then you set your <code>limit</code> accordingly.</p>

CC BY-SA 3.0
2015-08-14T19:10:23.577Z
Great answer! The only change I'd make is setting `limit = (entry.section.isHomepage() ? 1 : null)` beforehand (simply for code legibility). It may also be necessary on pages where `entry` is not defined.
Lindsey D
CC BY-SA 3.0
2015-08-14T20:33:17.610Z
freaking sweet!
Mark Busnelli Jr
CC BY-SA 3.0
2015-08-14T22:41:44.817Z
@LindseyD totally right about the code legibility. I will edit my answer
Marion Newlevant
CC BY-SA 3.0
2015-08-14T18:59:31.350Z
2015-08-14T22:42:57.300Z
2015-08-14T22:42:57.300Z
Marion Newlevant
<p>I only want one block on the home page, other pages can loop through all of them. Is there a more efficient way to write this?</p>

<pre><code>{% if craft.request.getSegment(1) is empty %}
        {% for block in memberships.memberships.limit(1) %}
        &lt;a href="{{ block.urlLocation }}" class="center" target="_blank"&gt;
            {% for asset in block.logo %}
                &lt;img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" /&gt;
            {% endfor %}
        &lt;/a&gt;
        {% endfor %}
    {% else %}
        {% for block in memberships.memberships %}
        &lt;a href="{{ block.urlLocation }}" class="center" target="_blank"&gt;
            {% for asset in block.logo %}
                &lt;img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" /&gt;
            {% endfor %}
        &lt;/a&gt;
        {% endfor %}
    {% endif %}
</code></pre>

<forloop>
Home page loop once, other pages loop through all
# Home page loop once, other pages loop through all

(Asked by: username_0 on 2015-08-14)

I only want one block on the home page, other pages can loop through all of them. Is there a more efficient way to write this?
{% if craft.request.getSegment(1) is empty %}
        {% for block in memberships.memberships.limit(1) %}
        
            {% for asset in block.logo %}
                
            {% endfor %}
        
        {% endfor %}
    {% else %}
        {% for block in memberships.memberships %}
        
            {% for asset in block.logo %}
                
            {% endfor %}
        
        {% endfor %}
    {% endif %}

**Tags:** forloop

---

## 1 answers

### Answer by username_1 on 2015-08-14. Score: 3 | Accepted answer

You could do it this way:
{% set blockLimit = entry.section.isHomepage() ? 1 : null %}

{% for block in memberships.memberships.limit(blockLimit) %}
  
    {% for asset in block.logo %}
      
    {% endfor %}
  
{% endfor %}

entry.section returns the SectionModel for the entry, and isHomepage() tells you whether that section is the homepage. Then you set your limit accordingly.

> **Comments:**
> **username_2**: Great answer! The only change I'd make is setting `limit = (entry.section.isHomepage() ? 1 : null)` beforehand (simply for code legibility). It may also be necessary on pages where `entry` is not defined.  
> **username_0**: freaking sweet!  
> **username_1**: @LindseyD totally right about the code legibility. I will edit my answer  

---

