Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T19:10:23.577Z
Answers.Text: 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.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T20:33:17.610Z
Answers.Text: freaking sweet!
Answers.UserDisplayName: Mark Busnelli Jr
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T22:41:44.817Z
Answers.Text: @LindseyD totally right about the code legibility. I will edit my answer
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T18:59:31.350Z
Answers.LastActivityDate: 2015-08-14T22:42:57.300Z
Answers.LastEditDate: 2015-08-14T22:42:57.300Z
Answers.UserDisplayName: Marion Newlevant
Body: <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>
  
Tags: <forloop>
Title: Home page loop once, other pages loop through all
ThreadText: # 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) %}
          <a href="{{ block.urlLocation }}" class="center" target="_blank">
              {% for asset in block.logo %}
                  <img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" />
              {% endfor %}
          </a>
          {% endfor %}
      {% else %}
          {% for block in memberships.memberships %}
          <a href="{{ block.urlLocation }}" class="center" target="_blank">
              {% for asset in block.logo %}
                  <img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" />
              {% endfor %}
          </a>
          {% 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) %}
    <a href="{{ block.urlLocation }}" class="center" target="_blank">
      {% for asset in block.logo %}
        <img src="{{ asset.url('memberships')}}" alt="{{ asset.title }}" />
      {% endfor %}
    </a>
  {% endfor %}
  
  ```
  
  `entry.section` returns the [SectionModel](http://buildwithcraft.com/docs/templating/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**: @username_2 totally right about the code legibility. I will edit my answer  
  
  ---
  
  