<p>The accepted answer only works for single-level structures, since <code>positionedBefore()</code> doesn't include its parent entry. I don't know why this is, maybe it's a bug? Anyways, the result is that first child entry will get the same "index" as their parent.</p>

<p>e.g.</p>

<ul>
<li>Home (1)</li>
<li>About (2)

<ul>
<li>Company (2)</li>
<li>Contact (3)</li>
<li>Location (4)</li>
</ul></li>
<li>Blog (6)</li>
</ul>

<p>Because <a href="https://github.com/craftcms/cms/blob/f5c3ea9a2f675392b9492978b3b0b5c5f01b6cf9/src/base/Element.php#L410" rel="nofollow noreferrer">structures are ordered by <code>lft</code> by default</a>, we can get a number corresponding to the entry's order in a structure by searching for entries in a section where its <code>lft</code> value is less than the reference entry.</p>

<pre><code>{% set position = craft.entries
  .section('MySection')
  .andWhere('lft &lt; "' ~ entry.lft ~ '"')
  .count() + 1
%}
</code></pre>

<p>This will result:</p>

<ul>
<li>Home (1)</li>
<li>About (2)

<ul>
<li>Company (3)</li>
<li>Contact (4)</li>
<li>Location (5)</li>
</ul></li>
<li>Blog (6)</li>
</ul>

<p>Update: Seems the issue with <code>positionedBefore()</code> is a bug after all. I think I'll still leave this here in case someone's using a version of Craft without the fix? Not sure if Craft 2 has the bug or not.</p>

CC BY-SA 4.0
2019-03-05T20:05:59.020Z
2019-03-05T20:15:36.310Z
2019-03-05T20:15:36.310Z
missmatsuko
<p>I found the clue for how to do this in the <a href="http://buildwithcraft.com/docs/templating/entrymodel#getPrevSibling">getPrevSibling</a> documentation:</p>

<blockquote>
  <p>getPrevSibling() will return the previous sibling whether or not it’s enabled. If you want to get the closest enabled sibling, you can do this instead:</p>
  
  <p><code>{% set prevSibling = craft.entries.positionedBefore(entry).order('lft desc').first() %}</code></p>
</blockquote>

<p>So this query will get the number of enabled previous siblings:</p>

<pre><code>{% set prevSibsCount = craft.entries.positionedBefore(entry).total() %}
</code></pre>

<p>And then your position will be that + 1:</p>

<pre><code>{% set position = prevSibsCount + 1 %}
{{ position }}
</code></pre>

CC BY-SA 3.0
2015-07-13T02:35:40.000Z
I didn't know how to do it either until you asked.
Marion Newlevant
CC BY-SA 3.0
2015-07-13T20:09:33.700Z
Wow. Good job in finding the best hidden `craft.entries` param, @Marion!
carlcs
CC BY-SA 3.0
2015-07-12T17:35:08.520Z
2015-07-12T17:35:08.520Z
Marion Newlevant
<p>I want to display the current entry's index - like in this post <a href="https://craftcms.stackexchange.com/questions/5125/get-entry-index">Get entry index</a>.</p>

<p>However my entries are in a structure section type not a channel, so they are not ordered by date.</p>

<p>Is is possible to edit the solution (below) from the above post so that it is not sorting the entries by date but rather retaining the custom order of the structure?</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 %}

{{ position }}
</code></pre>

<entry><entrymodel>
Get entry index - structure not channel
# Get entry index - structure not channel

(Asked by: username_0 on 2015-07-12)

I want to display the current entry's index - like in this post Get entry index.
However my entries are in a structure section type not a channel, so they are not ordered by date.
Is is possible to edit the solution (below) from the above post so that it is not sorting the entries by date but rather retaining the custom order of the structure?
{# 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 %}

{{ position }}

**Tags:** entry, entrymodel


---

## 2 answers

### Answer by username_2 on 2015-07-12. Score: 10 | Accepted answer

I found the clue for how to do this in the getPrevSibling documentation:

getPrevSibling() will return the previous sibling whether or not it’s enabled. If you want to get the closest enabled sibling, you can do this instead:
{% set prevSibling = craft.entries.positionedBefore(entry).order('lft desc').first() %}

So this query will get the number of enabled previous siblings:
{% set prevSibsCount = craft.entries.positionedBefore(entry).total() %}

And then your position will be that + 1:
{% set position = prevSibsCount + 1 %}
{{ position }}

> **Comments:**
> **username_2**: I didn't know how to do it either until you asked.  
> **username_3**: Wow. Good job in finding the best hidden `craft.entries` param, @Marion!  

---

### Answer by username_1 on 2019-03-05. Score: 1

The accepted answer only works for single-level structures, since positionedBefore() doesn't include its parent entry. I don't know why this is, maybe it's a bug? Anyways, the result is that first child entry will get the same "index" as their parent.
e.g.

Home (1)
About (2)


Company (2)
Contact (3)
Location (4)

Blog (6)

Because structures are ordered by lft by default, we can get a number corresponding to the entry's order in a structure by searching for entries in a section where its lft value is less than the reference entry.
{% set position = craft.entries
  .section('MySection')
  .andWhere('lft < "' ~ entry.lft ~ '"')
  .count() + 1
%}

This will result:

Home (1)
About (2)


Company (3)
Contact (4)
Location (5)

Blog (6)

Update: Seems the issue with positionedBefore() is a bug after all. I think I'll still leave this here in case someone's using a version of Craft without the fix? Not sure if Craft 2 has the bug or not.


---

