You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/5.x/reference/twig/tags.md
+11-1Lines changed: 11 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -558,7 +558,10 @@ This tag works identically to the [namespace](filters.md#namespace) filter, exce
558
558
This tag helps create a hierarchical navigation menu for entries in a [Structure section](../element-types/entries.md#section-types) or a [Category Group](../element-types/categories.md).
559
559
560
560
```twig
561
-
{% set entries = craft.entries().section('pages').all() %}
561
+
{% set entries = craft.entries()
562
+
.section('pages')
563
+
.orderBy('lft ASC')
564
+
.all() %}
562
565
563
566
<ul id="nav">
564
567
{% nav entry in entries %}
@@ -574,6 +577,13 @@ This tag helps create a hierarchical navigation menu for entries in a [Structure
574
577
</ul>
575
578
```
576
579
580
+
Note that we are doing two things to ensure the entire tree of pages is displayed correctly:
581
+
582
+
- The query fetches _all_ elements: we grab everything in the structure in a single, flat array.
583
+
- Ordering by `lft ASC`: this ensures that the pages are properly sorted, such that every child appears after its parent.
584
+
585
+
Craft then takes this one-dimensional array of elements and evaluates the opening and closing template content (fragments on either side of `{% children %}`, within `{% ifchildren %}`) a number of times appropriate to each item’s `level` relative to the last item.
586
+
577
587
::: tip
578
588
The `{% nav %}` tag should _only_ be used in times when you want to show elements in a hierarchical list. If you want to show elements in a flat list, use a [for](https://twig.symfony.com/doc/tags/for.html) tag instead.
0 commit comments