Blog Permalink Format

Is it possible to structure the slug’s for a blog in the format /blog/YEAR/MONTH/DAY/title? That is, the blog is contained in the blog directory within pages, but it uses the same folder names as the Antimatter theme (eg. 2014-03-10-sample-post) for posts rather than a folder-hierarchy of /blog/2014/03/10/sample-post/.

I have found a solution using routes, but am having trouble setting them up correctly. What I have is this:

The Regex pattern ^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])-(.*) will match 2014-03-10-sample-post and group this as year, month, date, title (see RegExr).

The folders with posts are in /home, and named by the match above. Thus, to route them into /blog/* I tried:

routes:
   /blog/^([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])-(.*): '/home/$1/$2/$3/$4'

But this fails. Have I got the Regex backwards, or is there some magic I need to do with the routing itself?

The regex routes might be an option. Assuming you have some page structure like this:

01.home/
 - 2014-03-10-sample-post/
 - 2014-04-07-another-post/
 ...

You could setup a rewrite route like this:

routes:
   '/blog\/(\d{4})\/(\d{2})\/(\d{2})\/(.*)': '/home/$1-$2-$3-$4'

That should work… BTW, you can tweak/play with the regex here: https://regex101.com/r/dW1tU0/1

That is excellent, exactly the outcome I had hope for - and the routes are also easy to expand into categorical templates (year, month, date).

As an alternative approach, say I have a folder /blog with the substructure /year/month/day/post where /postis each page. There are no files in the year, month, or day folders - they are only used for semantic structure, but each /post folder has a post.md and associated media.

To iterate over them (in index.html.twig), I use collections (from Antimatter-theme if memory serves):

{% for post in collection %}
    {% include 'partials/article.html.twig' %}
{% endfor %}

{% if config.plugins.pagination.enabled and collection.params.pagination %}
    {% include 'partials/pagination.html.twig' with {'pagination':collection.params.pagination} %}
{% endif %}

Where article.html.twigdisplays details of each page. The header of index.html.twig is set as:

content:
    items: @self.descendants
    order:
        by: date
        dir: desc
    limit: 5
    pagination: true

Which displays each page’s detail logically, where 2015 precedes 2016, month 11 precedes month 12, etc - due to the order of numbers. However, date is not actually a valid order here as none of the pages have a date set in their header (that would be superfluous, given the structure). Thus, reversing the order by setting dir: asc causes a Twig error.

How can I reverse the logical ordering of the descendant pages, so that 2016 precedes 2015, month 12 precedes month 11, day 28 precedes day 27, etc?

That is., allowing the collection to treat this folder hierarchy as actual dates, or at least reversing the order of the loop throughout.

Eeally the best way is simply to add the dates into the page headers. Then all the sorting by date functionality would just work. Sure it’s a little overhead initially, but wouldn’t be much effort going forward.

Is there no twig override for the headers, so I could for example extract it from the URL and set it from there?

Well, that can be done, but it would need to be done as a plugin using the onPageContentProcessed event. Read more about Events in the docs.

You can look at the shortcode-core or the smartypants plugins to see an example of this event being used.