Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Get -recursively- all pages

Solved by Ben Bricker View solution

Started by Usk70 9 years ago · 3 replies · 605 views
9 years ago

I have the following structure:

TXT
 /blog
    /area1
         /blog_entry1
             blog.md
         /blog_entry2
             blog.md
         /blog_entry3
             blog.md
    /area2
         /blog_entry4
             blog.md
    /area3
         /blog_entry5
             blog.md
         /blog_entry6
             blog.md

I have seen this recipe in twig:
Get last 5 post entries

But as far as I have tried, it does not work recursively.

Is there any option to recursively get the last 5 post entries of all .md files child of /blog ?
(which in this case would be blog_entry1 to blog_entry6)

9 years ago

There is several ways of solving this problem.
Basically, you have to adjust your collection in order to get these pages:

YAML
content:
  items:
    - '@page.children' : /area1
    - '@page.children' : /area2
    - '@page.children' : /area3
  limit: 5

Then you just have to write:

TWIG
{% for item in page.collection %}
{{item.title }}
{% endfor %}

Alternatively you could write:

TWIG
{% for area in page.find('/blog').children %}
  {% for article in area.children %}
    {{ article.title }}
  {% endfor %}
{% endfor %}

Not tested, but it might work

👍 1
9 years ago

Also, there's a recipe for recursively iterating through pages and media. You'd have to pass the resulting array back through Twig, though.

👍 1
9 years ago Solution

You could use descendants instead of children.

TWIG
{% for item in page.collection({'items': '@self.descendants','order': {'by': 'date','dir': 'desc'}}) %}
👍 2

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 51 7 hours ago
Support · by Anna, 2 days ago
2 58 10 hours ago
Support · by Justin Young, 11 hours ago
1 30 11 hours ago
Support · by Duc , 1 week ago
2 63 5 days ago
Support · by Colin Hume, 1 week ago
2 55 5 days ago