0

I thought this was going to be easy; showing the order number of children against their parent, but the only answer I can find is in this thread

and I'm unsure how to implement this into my code.

{% for chapter in entry.descendants %}
<div class="part"><span>Part {{ chapter.orderNumber | numbersToWords }}</span></div>
</div>                      
{% endfor %}

Resulting in: Part One

Note: numbersToWords is the Helpers Plugin by carlcs

1 Answer 1

1

If you're getting all the descendants in the for loop, you can use loop to get the index of the current iteration:

{% for chapter in entry.descendants %}
    <div class="part"><span>Part {{ loop.index | numbersToWords }}</span></div>
{% endfor %}

See the documentation for loop in the Twig docs for more details.

This gives you the index of the current iteration in the for loop though, not the actual position of the child in the structure. So if you query only a subset of the descendants, or reorder and manipulate the descendants, this won't work.

1
  • Thanks André, that works nicely as each chapter has a subtitle and the part order number matches the Structure and the custom editor's (drag & drop) arrangement.
    – Nutmeg
    Commented Nov 14, 2016 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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