How to Generate Chronological Blog Archives in Hugo?


#1

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?


#2

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.


#3

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.


#4

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).


#5

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.


#6

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.


#7

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:


#8

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

So if you feel like sharing please do.