How to Generate Chronological Blog Archives in Hugo?

Is it possible to have the path listing pages generated by hugo. For instance, I have a permalink setting of

post = "/:year/:month/:slug/"

And this generates posts at e.g. /2018/08/a-post/

But it doesn’t generate any listing pages at e.g /2018/08/ or /2018/

Is this something you have to build manually by creating the folders and _index.md pages?

Blog archives functionality is not built into Hugo by default. It is possible to do it and there have been relevant discussions in the Forum, use the search button for more.

Thanks for the search terms. I’d done a bit of searching on the site but didn’t know the right words apparently.

At the end of a post I found https://blog.atj.me/2017/10/generate-yearly-and-monthly-archive-pages-with-hugo-sections/ which appears to get me all the way there. It’s a bit gnarly and involves running a script to automate the content creation but it gets me closest to what I want. i.e. a page at /2018/10/ that shows me the posts made in that month.

It’s a shame hugo doesn’t do this. Creating a permalink url structure that returns 404’s is not all that intuitive.

Surely there is a simpler way.

Like in this Forum thread from last year:

A word of advice:
Never go for solutions that require JavaScript to generate post lists in Hugo.

That article is all over the place. SMH (shaking my head).

The thing is, I just want listing pages at the paths the permalink implies should exist - standalone archive pages don’t do that.

It looks like the script is optional and generates the content that drives the creation of pages at the correct path. You could do that manually I guess.

Ok @mrwonka

Here is how to do this.

Supposing that your content is under a section called /post/

Contents of config.toml

[taxonomies]
year = "year"
month = "month"

[permalinks]
post = "/post/:year/:month/:slug/"
year = "/post/:slug/"
month = "/post/:slug/"

Basically with the above we configure the section’s permalinks to use the :year and :month values from the date parameter of a content file along with its :slug (usually this is either derived from the content’s file name or from a slug front matter parameter).

Then we will configure the permalinks of the year and month taxonomies to use the section name in this case post and the variable :slug that in this context stands for the contents of the year and month keys in the front mater of every content file.

And then in every piece of content we will include the following front matter parameters (in this case this is about a post that was published in November 2014.

year = "2014"
month = "2014/11"

This is the simplest way that I know for rendering chronological blog archives in Hugo (without the funky stuff).

There is always room for improvement.

So I am tagging the following good people who might chime in if they feel like it.

CC/ @bep @kaushalmodi

P.S. I renamed the thread title to something more appropriate.

1 Like

I’ve done what you listed above (with the exception of removing “/post” from the permalink generation and it now all works pretty good. I can get some logic in the list.html template to differ the title based on the source taxonomy which fits my needs.

It’s a shame that I need 3 front matter items that all essentially refer to the date of publication but for now I guess it can’t be helped.

Thanks :slight_smile:

1 Like

It would be useful to see the template logic you used.

So if you feel like sharing please do.

Here is my config

[taxonomies]
    year = "year"
    month = "month"
[permalinks]
    post = "/posts/:year/:month/:slug/"
    year = "/posts/:slug/"
    month = "/posts/:slug/"

Here is my one of the post in /posts directory (top part)

+++
title = "Cameras,Rolls,Photos and Albums"
slug = "2002-05-21-cameras-rolls-photos-and-albums"
published = 2002-05-21T14:55:00+05:30
author = "Santanu Misra"
tags = [ "Paris", "picture",]
+++

When i try to access

http://localhost:1313/posts/2002/ or http://localhost:1313/2002/

I get 404 errror

Of course you are getting a 404.

Do read the above post more carefully. You are not following it to the letter, but rather you are doing something else.

Also please do not resurrect old posts from 2 years ago.