<p>I was able to limit my entries by using the loop.index function</p>

<p>Code below:</p>

<pre><code>{% for entry in craft.entries.section('insight')  %}

        {# Limit output to the first 2 #}   
        {% if loop.index &lt;= 2 %}  

            &lt;div class="insight"&gt;
                &lt;a href="{{ entry.url }}"&gt;
                    &lt;h4&gt;Global Insights *latest random insight* &lt;/h4&gt;
                    &lt;h3&gt;{{ entry.title }}&lt;/h3&gt;
                    &lt;div class="details"&gt;
                        &lt;span class="author"&gt;{{entry.writer}}&lt;/span&gt;
                    &lt;/div&gt;
                &lt;/a&gt;
            &lt;/div&gt;

        {% endif %}

    {% endfor %}
</code></pre>

CC BY-SA 3.0
2017-02-09T07:08:07.203Z
Great for you! But honestly I do not see why the normal limit wouldn't work here... In my opinion that would still be the cleanest and fastest solution.
Tom De Smet
CC BY-SA 3.0
2017-02-09T10:27:41.143Z
Its because of the elemental criteria model apparently, using limit was limiting how many entries my code was checking instead of how many it outputs, for example limit(3) was only running the code through the first 3 entries in that section. Weird but i found a work around. Thanks for your help tho!
Davabo
CC BY-SA 3.0
2017-02-08T17:46:39.270Z
2017-02-08T17:46:39.270Z
Davabo
<p>What you probably want is to order them by date and than select the first.</p>

<pre><code>{% set news = craft.entries.section('news').order('postDate desc').first() %}
</code></pre>

<p>And if you want to use limit somewhere (but I don't think this is what you are trying to get here) you should add limit(1) to this <code>{% set news = craft.entries.section('news').limit(1) %}</code></p>

CC BY-SA 3.0
2017-02-08T16:35:30.087Z
Sadly this doesnt help, thanks for the reply tho.
Davabo
CC BY-SA 3.0
2017-02-08T17:07:08.580Z
What do you get as a result? Can't you combine all the conditions in one set entry by the way? To reduce code.
Tom De Smet
CC BY-SA 3.0
2017-02-08T14:48:22.560Z
2017-02-08T14:48:22.560Z
Tom De Smet
<p>I'm trying to limit the amount of entries on my page.</p>

<p>Currently I want 1 news entry (based on if the checkbox is ticked, there could be 10 entries with the checkbox ticked but I only want to show 1 (the latest)) The issue is that the <code>limit()</code> twig function doesn't work with my <code>elementCriteriaModel</code>.</p>

<p>My code:</p>

<pre><code>{# Checking if any news entries have the FeaturedEntry field completed #}
{% for entry in craft.entries.section('news').level(2) %}

    {% if entry.featured.contains('isFeatured') %}

        {% set news = craft.entries.section('news') %}

            &lt;div class="insight orange"&gt;
                &lt;a href="{{ entry.url }}"&gt;
                    &lt;h4&gt;News *Chosen*&lt;/h4&gt;
                    &lt;h3&gt;{{ entry.title }}&lt;/h3&gt;
                    &lt;div class="details"&gt;
                        &lt;span class="author"&gt;{{entry.author}}&lt;/span&gt;
                    &lt;/div&gt;
                &lt;/a&gt;
            &lt;/div&gt;

    {% else %}
            Do Something Else Here
</code></pre>

<p>If I attach the limit function to <code>{% for entry in craft.entries.section('news').level(2).limit(1) %}</code>, this only limits the amount of entries my selector can check for the featured field. If i put <code>limit(1)</code> it only checks the first news entry for the field.</p>

<p>I know i can use the slice function but I'm having some trouble inserting it into the right place.</p>

<p>Any help would be very appreciated.</p>

<entry>
How to limit the amount of entries that is output when limit doesn't work
# How to limit the amount of entries that is output when limit doesn't work

(Asked by: username_0 on 2017-02-08)

I'm trying to limit the amount of entries on my page.
Currently I want 1 news entry (based on if the checkbox is ticked, there could be 10 entries with the checkbox ticked but I only want to show 1 (the latest)) The issue is that the limit() twig function doesn't work with my elementCriteriaModel.
My code:
{# Checking if any news entries have the FeaturedEntry field completed #}
{% for entry in craft.entries.section('news').level(2) %}

    {% if entry.featured.contains('isFeatured') %}

        {% set news = craft.entries.section('news') %}

            
                
                    News *Chosen*
                    {{ entry.title }}
                    
                        {{entry.author}}
                    
                
            

    {% else %}
            Do Something Else Here

If I attach the limit function to {% for entry in craft.entries.section('news').level(2).limit(1) %}, this only limits the amount of entries my selector can check for the featured field. If i put limit(1) it only checks the first news entry for the field.
I know i can use the slice function but I'm having some trouble inserting it into the right place.
Any help would be very appreciated.

**Tags:** entry


---

## 2 answers

### Answer by username_0 on 2017-02-08. Score: 1 | Accepted answer

I was able to limit my entries by using the loop.index function
Code below:
{% for entry in craft.entries.section('insight')  %}

        {# Limit output to the first 2 #}   
        {% if loop.index <= 2 %}  

            
                
                    Global Insights *latest random insight* 
                    {{ entry.title }}
                    
                        {{entry.writer}}
                    
                
            

        {% endif %}

    {% endfor %}

> **Comments:**
> **username_1**: Great for you! But honestly I do not see why the normal limit wouldn't work here... In my opinion that would still be the cleanest and fastest solution.  
> **username_0**: Its because of the elemental criteria model apparently, using limit was limiting how many entries my code was checking instead of how many it outputs, for example limit(3) was only running the code through the first 3 entries in that section. Weird but i found a work around. Thanks for your help tho!  

---

### Answer by username_1 on 2017-02-08. Score: 1

What you probably want is to order them by date and than select the first.
{% set news = craft.entries.section('news').order('postDate desc').first() %}

And if you want to use limit somewhere (but I don't think this is what you are trying to get here) you should add limit(1) to this {% set news = craft.entries.section('news').limit(1) %}

> **Comments:**
> **username_0**: Sadly this doesnt help, thanks for the reply tho.  
> **username_1**: What do you get as a result? Can't you combine all the conditions in one set entry by the way? To reduce code.  

---

