3

Is there a way in Craft to get the order property of the current entry when it's in a structure? For example say you have the following structure:

  • Parent Page
    • Child One
    • Child Two
    • Child Three

Is there a way to get what order property Child Two is in relation to its siblings so in this case it would be 1 (zero based)?

In my templates I am wanting to be able to grab the order property for the current entry by doing something like {{ entry.getOrder() }}.

6

There's a positionedBefore parameter for craft.entries. Use this with a criteria model filtered to show the entries you are interested in only.

{% set parent = entry.getParent() %}
{% set siblingsIds = parent.getChildren().ids() %}

{% set positionedBefore = craft.entries({
    id: siblingsIds,
    order: 'lft desc',
    positionedBefore: entry,
}).total() %}

With the first two lines I'm getting the IDs of all of the entry's siblings. I then prepare a craft.entries criteria model using the siblings' IDs with the id parameter to query just for those entries, order them by lft in descending order (that's the db column Craft stores position info) and further filter with positionedBefore to get just the entries before entry.

Calling the total method returns the number of entries found.

| improve this answer | |
  • Thank you @carlcs that seems to work perfect, could you just expand on your answer a little to help me understand exactly what its doing? – Gareth Jan 14 '16 at 10:51
  • Very clever, I get it now. I didn't know about the lft column. Thank you again! – Gareth Jan 14 '16 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.