URL management with custom .Params

I have the following structure:

content
    |-- books
       |-- _index.md
       |-- book1.md
       |-- book2.md
       |-- book3.md
       ...
       |-- book500.md

layout
     |-- books
           |-- list.html
           |-- single.html

Front matter for each book:
+++
date = ""
title = ""
description = "
genre = ""
++

With this in place, each book can be found at /books/filename/.

However, I’d like to change the URL for each file so it includes the genre, a custom front matter variable that I’ve created and can be found in each file.

This is the structure I’d like: /books/genre/filename/

Example: /books/fiction/book1/ and /books/adventure/book2/

I know this can be achieved by changing the books content structure, but this is not something I want to do. Adding a slug to each file, not sure if this works, but even if it would, doesn’t seem optimal.

I’ve had a look at the docs, and it seems I need to configure permalinks, but it doesn’t seem to allow custom configuration values, like :genre.

Any suggestions on how I can achieve this structure?

No.

Whenever I see words like “genre” or “type” or I immediately think of the taxonomy system. Would you consider doing something like this?

config.toml

[taxonomies]
genre = "genres"

[permalinks]
genres = "/books/:slug"

This would allow you to:

  1. Visit /books/science-fiction/ to see all the science fiction novels
  2. Visit /books/crime/ to see all the crime novels
  3. Visit /books/foo to see all the science fiction crime novels (presuming you built a list page for this)

The same book could appear on all three pages. For example, Altered Carbon is a science fiction mystery novel, and would have front matter such as:

+++
title = "Altered Carbon"
author = "Richard K. Morgan"
genres = ["crime","science fiction"]
+++

But the book itself would still have the URL /books/altered-carbon/.

2 Likes

Upon further consideration, do the same with authors:

config.toml

[taxonomies]
author = "authors"
genre = "genres"

[permalinks]
authors = "/books/author/:slug"
genres = "/books/genre/:slug"

front matter

+++
title = "The Talisman"
authors = ["Stephen King", "Peter Straub"]
genres = ["fantasy", "horror"]
+++

One book. Two authors. Two genres. And if you like Stephen King visit /books/author/stephen-king/ to see what else he’s written.

1 Like

Thank you for the informative reply!

Interesting. I’ll need to take a closer look and see if there may be an issue with using taxonomies in my code. If not, then this seems to be an excellent approach.

Many thanks.