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.

General

Get most recent posts when using sub-folders

Started by Trevor Robertson 7 years ago · 0 replies · 387 views
7 years ago

This is easy to do if you just have a standard blog setup, as then you can use the recipe found here. However I setup sub-folders for my blog (a folder for each year), so I found this to be trickier. I have a solution that works, so I'm posting that below to help anyone else. But then I have questions below about it...

TWIG
{% set blog_posts =
  page.collection({
    'items':{
      '@page.descendants': '/blog'
    },
    'filter': {
      'type': 'item'
    },
    'order': {
        'by': 'date',
        'dir': 'desc'
    },
    'limit': 5,
  })
%}

{% for post in blog_posts %}
  <p>{{ post.title }}</p>
  <a href="{{ post.url }}">LINK!</a>"
{% endfor %}

So while this works fine I wondered if this was the best way to do it? Also, on a related note, is it even a good practice to use sub-folders for your blog? I thought it might be nice to have folder organization for years to keep things tidy but it seems to create more issues than it's worth. Any advice from more experienced users would be great...

👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago