Answers.Body: <p>There's no way to do that without a query to the DB.</p>
  
  <p>Not sure if this is really faster, but try to only query for the IDs and see how this performs for you. Another thing you could do, is to add your conditional directly to the for loop tag:</p>
  
  <pre><code>{% set entryIds = craft.entries.section('news').order('postDate asc').ids() %}
  
  {# Get the position of this entry's ID within our IDs array #}
  {% set position = '' %}
  {% for key, value in entryIds if value == entry.id %}
      {% set position = key + 1 %}
  {% endfor %}
  
  {# Get the amount of total entries #}
  {% set total = entryIds|length %}
  
  {{ entry.title }} (no. {{ position }} / {{ total }})
  </code></pre>
  
  <p>If you are not interested in the total number, here's another way to get the position. The syntax is simpler, though it does not perform better, as you still need to query for the entries:</p>
  
  <pre><code>{# Get this entry + all entries posted before it #}
  {% set postDateParam = '&lt;= ' ~ entry.postDate|date('c') %}
  {% set entryIds = craft.entries.section('news').order('postDate asc').postDate(postDateParam).ids() %}
  
  {# Count them #}
  {% set position = entryIds|length %}
  
  {{ entry.title }} (no. {{ position }})
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-08T14:51:39.400Z
Answers.Text: Would it be possible to do a custom DB query to get all news items that have a postDate lower than the one I'm currently on? If so, I could just do `{{ olderEntries | length }}`, right?
Answers.UserDisplayName: peirix
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-09T01:40:44.097Z
Answers.Text: Yep. But then you would need two queries. One for position / older entries and one for total. My solution gives you both numbers (+ 3rd using math) with just one IDs query. The Twig code is fast!!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-09T10:01:40.333Z
Answers.Text: Well, I don't really need to know the total. All I need is the text "no. 2". And, yeah, I've noticed that twig is fast. It just kinda feels dirty to use a loop to find that number.
Answers.UserDisplayName: peirix
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-09T10:24:40.803Z
Answers.Text: It only enters the loop once because of the conditional added to the syntax. But yeah, if you don't need that other numbers, you can leave it out. Will update the answer, @peirix!
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T23:26:15.057Z
Answers.LastActivityDate: 2014-12-09T10:23:26.703Z
Answers.LastEditDate: 2014-12-09T10:23:26.703Z
Answers.UserDisplayName: carlcs
Answers.Body: <p>You can use Twig's <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="nofollow">attribute</a> function.</p>
  
  <p>For array's it is 0-indexed, so if you want the first element, it's:</p>
  
  <pre><code>{% set entries = craft.entries.section('news').find() %}
  
  {% if entries|length %}
      {{ attribute(entries, 0).title }}
  {% endif %}
  </code></pre>
  
  <p>The second would be <code>{{ attribute(entries, 1).title }}</code>, etc.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-07T12:34:12.987Z
Answers.Text: Hm. Think you may have misunderstood the question. I'm not looking to get an entry given an index. It's the other way around. I'm on a entry page, and I want to find the current entry's index. So when I'm viewing a news item, I want to find out how many news items that have been posted before this one.
Answers.UserDisplayName: peirix
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-07T16:13:21.727Z
Answers.Text: Ahh, I misunderstood. @carlcs's solution looks good, then. http://craftcms.stackexchange.com/a/5138/57
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-06T17:10:16.170Z
Answers.LastActivityDate: 2014-12-06T17:10:16.170Z
Answers.UserDisplayName: Brad Bell
Body: <p>Is there a way to find the current entry's index? So I want to know how many news items has been posted before the current one.</p>
  
  <p>So when I'm looking at the first news item, it should show "no.1", and for news item 15 show "no.15" and so on.</p>
  
  <p>What I have currently is getting all news items, ordered by date, loop through them until I find the one I'm on, and show the <code>loop.index</code>, which seems like it might be a bit slow, especially when we get to item number 100, 200 and so on.</p>
  
Tags: <entrymodel>
Title: Get entry index
ThreadText: # Get entry index
  
  (Asked by: username_0 on 2014-12-06)
  
  Is there a way to find the current entry's index? So I want to know how many news items has been posted before the current one.
  
  So when I'm looking at the first news item, it should show "no.1", and for news item 15 show "no.15" and so on.
  
  What I have currently is getting all news items, ordered by date, loop through them until I find the one I'm on, and show the `loop.index`, which seems like it might be a bit slow, especially when we get to item number 100, 200 and so on.
  
  **Tags:** entrymodel
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-12-06. Score: 2 | Accepted answer
  
  There's no way to do that without a query to the DB.
  
  Not sure if this is really faster, but try to only query for the IDs and see how this performs for you. Another thing you could do, is to add your conditional directly to the for loop tag:
  
  ```
  {% set entryIds = craft.entries.section('news').order('postDate asc').ids() %}
  
  {# Get the position of this entry's ID within our IDs array #}
  {% set position = '' %}
  {% for key, value in entryIds if value == entry.id %}
      {% set position = key + 1 %}
  {% endfor %}
  
  {# Get the amount of total entries #}
  {% set total = entryIds|length %}
  
  {{ entry.title }} (no. {{ position }} / {{ total }})
  
  ```
  
  If you are not interested in the total number, here's another way to get the position. The syntax is simpler, though it does not perform better, as you still need to query for the entries:
  
  ```
  {# Get this entry + all entries posted before it #}
  {% set postDateParam = '<= ' ~ entry.postDate|date('c') %}
  {% set entryIds = craft.entries.section('news').order('postDate asc').postDate(postDateParam).ids() %}
  
  {# Count them #}
  {% set position = entryIds|length %}
  
  {{ entry.title }} (no. {{ position }})
  
  ```
  
  > **Comments:**
  > **username_0**: Would it be possible to do a custom DB query to get all news items that have a postDate lower than the one I'm currently on? If so, I could just do `{{ olderEntries | length }}`, right?  
  > **username_1**: Yep. But then you would need two queries. One for position / older entries and one for total. My solution gives you both numbers (+ 3rd using math) with just one IDs query. The Twig code is fast!!  
  > **username_0**: Well, I don't really need to know the total. All I need is the text "no. 2". And, yeah, I've noticed that twig is fast. It just kinda feels dirty to use a loop to find that number.  
  > **username_1**: It only enters the loop once because of the conditional added to the syntax. But yeah, if you don't need that other numbers, you can leave it out. Will update the answer, @username_0!  
  
  ---
  
  ### Answer by username_2 on 2014-12-06. Score: 0
  
  You can use Twig's [attribute](http://twig.sensiolabs.org/doc/functions/attribute.html) function.
  
  For array's it is 0-indexed, so if you want the first element, it's:
  
  ```
  {% set entries = craft.entries.section('news').find() %}
  
  {% if entries|length %}
      {{ attribute(entries, 0).title }}
  {% endif %}
  
  ```
  
  The second would be `{{ attribute(entries, 1).title }}`, etc.
  
  > **Comments:**
  > **username_0**: Hm. Think you may have misunderstood the question. I'm not looking to get an entry given an index. It's the other way around. I'm on a entry page, and I want to find the current entry's index. So when I'm viewing a news item, I want to find out how many news items that have been posted before this one.  
  > **username_2**: Ahh, I misunderstood. @username_1's solution looks good, then. http://craftcms.stackexchange.com/a/5138/57  
  
  ---
  
  