Annual and Monthly archive. #5487
Replies: 1 comment
-
This sad and lonely post turned up at the top of the search result when I tried to make monthly archives with Statamic, and after a day of suffering I thought I'd relieve the next web wanderer. First you need to set up routes that redirect the archive urls to a template routes/web.php
and then in the template you can do
but getting the dates correct was a bit of a mindfuck since they appear to be exclusive ranges, and there doesn't seem to be a good way to turn string variables into date objects to manipulate. But the good news is until/since use PHP's
that is, it gives it a year or year-month and then combines that with the relative date arithmetic in string form. You gotta love PHP for supporting outlandish stuff like this in the standard library. So you'd get something like "2025-01 last day of previous month" which would result in the date 2024-12-31 so that you include the first day of the year in your archive range. Maybe I'm missing something obvious like an "inclusive=true" flag somewhere but that's what I landed on. Hope it helps someone. For generating the actual list you can just use group_by like so
I haven't managed to have a nested loop of year->month but I haven't tried very hard. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Does anybody have any good code snippets for producing a dynamic list of archives by year > month ?
Beta Was this translation helpful? Give feedback.
All reactions