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

Can't list page collection in date order

Started by Muut Archive 10 years ago · 4 replies · 1238 views
10 years ago

I want to list a collection by date. I believe I have followed the docs http://learn.getgrav.org/content/collections but the output is not ordered by date.

I have set up:

YAML
content:
    items: '@self.children'
    order:
        by: date
        dir: asc

Instances look like this:

YAML
title: Pride and Prejudice
author: Jane Austen
date: 1813

The template lists the items in the page collection in the usual way:

TWIG
{% for child in page.collection %} 
    <li>
    <a href="{{ child.route }}">{{ child.title }}</a> 
    {{ child.header.author }} {{ child.header.date }}
    </li>
{% endfor %}

The items are listed in an order that I don't understand, but it's certainly not date order. Nor is it random, because if I change asc to desc I get the same order reversed.

Anyone know what I'm doing wrong?

10 years ago

date: is a reserved Grav header that expects dates to be in a strtotime compatible format: http://learn.getgrav.org/content/headers#date

Easiest solution is to change your dates so the format is something that Grav is going to understand.

I'll have to check the code to see if we have support for a custom field. I think I added it at some point, but didn't document/test it fully.

10 years ago

Ah. I didn't realise that. Suppose I should have guessed. But ... I changed date to pub_datethroughout, and I'm still not getting the expected ordering. Nor when I try to order by author. In fact, I now see they're being listed in order of title, whether I ask for ordering by date or by author.

Just to confirm, this is the header code:

YAML

title: 'By author'
content:
    items: '@self.children'
    order:
        by: author
        dir: asc
```. 
Looking back at the docs on collection pages, i wonder whether ordering only applies to component pages of a modular page, rather than to ordinary children of an ordinary non-modular collection? That would be a nuisance.
10 years ago

Ok the problem is that author is not a valid type either. I did a bit of research and realized this already documented in the Collection Documentation

Also what is documented is that you can order: by: header.X- So this means you should be able to define:

YAML
title: 'By author'
content:
    items: '@self.children'
    order:
        by: header.author
        dir: asc

or

YAML
title: 'By author'
content:
    items: '@self.children'
    order: 
        by: header.pub_date
        dir: asc

I knew i had done this work already!

10 years ago

Tut tut, Thomas, rtfm. Sorry for missing that. Thanks - yet again.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2956 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago