Skip to main content

Gérer plusieurs auteurs grâce à la taxonomie

· 5 min read
j0rdan-m

Not all themes have the ability to manage multiple authors within a blog. And I'm probably a bad observer, because I didn't see, in the presentation of the available themes, if a theme natively manages several authors or not.

It's quite unpleasant to have found THE perfect theme, and that a simple functionality is missing. So I decided to "trick", and use the native taxonomy management, in order to register several authors on a blog. And for the time being, it works pretty well.

To write this article, I was inspired by the following tutorial: How to create author pages.

I take care to complete it only on the points for which I had to experiment a lot.

Uses of taxonomies

There are two standard taxonomies in Hugo:

  • Tags;
  • Categories.

To use Hugo's standard taxonomies, or those native to the chosen theme, it's very simple, you just have to add to your archetype the following elements:

---
tags:
- faire son site
- hugo-tips
categories:
- hugo
---

LThe principle is simple: as soon as you add a category or a tag, and the theme manages it well, you will find a page http://[votresite]/tags/ and another one called /catégories/ that list tags and categories, and a page per item listing the articles that mention the item in their archetypes.

Now let's see how to create a new taxonomy that would open the possibility to add one or more authors to an article.

Creating a taxonomy

Each taxonomy has two templates:

  • list.html, which corresponds to the template of the page that will describe one of the items (an author, for us, and all the articles linked to it)
  • terms.html, which corresponds to the page that will list all the items of a taxonomy (all the authors, therefore)

In our case, we will create an author taxonomy. It will be post authors, and not book authors, as in the example of the previous link.

We are going to work in two tree structures:

  • the content folder, logically at the root of the hugo site ;
  • the layout folder of the theme used by the site.

The data concerning the items of the taxonomy

In the content folder, we will create a folder for each author, in which a _index.md file will be placed,

content
└── authors/
├── j0rdan-m/
│ └── _index.md
└── author-2/
└── _index.md

Each _index.md describes an author, and is composed as follows:

---
name: j0rdan-m
photo: 'https://j0rdan-m.gitlab.io/blog/pearlbonnet.png'
twitter: '@j0rdan_m'
---
j0rdan is an author of the blog

Meta-data

The data concerning the authors are ready, now we have to set up the meta-data.

To do this, we will have to create an outside folder in the layout of our theme (and not the taxonomies folder, which may be surprising).

my_theme/
└── layout/
└── authors/
└── list.html
└── terms.html

wThe list.html file does not list the authors, but the articles that mention the authors (or any other choice of taxonomy).

So, here is the basic content of the list.hml file:

<h1>{{ .Params.name }}</h1>
<img src="{{ .Params.photo }}" alt=""/>

<h2>Bio</h2>
{{ .Content }}
{{ with .Params.twitter }}
<p>
<a href="https://twitter.com/{{ substr . 1 }}">
Follow {{ $.Params.name }} on Twitter
</a>
</p>
{{ end }}

<h2>Articles</h2>
<ul>
{{ range .Data.Pages }}
<li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}
</ul>

The terms.html file is the template of the page that will list all the authors, and propose a link for each one:

<h1>Authors</h1>
<ul>
{{ range .Data.Pages }}
<li><a href="{{ .Permalink }}">{{ .Params.name }}</a></li>
{{ end }}
</ul>

Warning, it will be necessary to integrate upstream and downstream of the proposed codes the content of the theme, taking example on the default page (probably in themes/my_theme/layouts/_default/single.html)

The theme used by this blog offered a nice comment to isolate the theme info from the content (thanks to the <section></section> tag. Another theme I use mentions

)

Since we are on this single.html page, we will be able to add a mention of the authors:

{{- range .Params.authors }}
{{- with $.Site.GetPage "taxonomyTerm" (printf "authors/%s" (urlize .)) }}
<figure>
<img src="{{ .Params.photo }}" alt=""/>
<figcaption>
<a href="{{ .Permalink }}">{{ .Params.name }}</a>
</figcaption>
</figure>
{{ end }}
{{ end }}

This mention is to be added at the place of the page where you want it to appear, of course.

Finally, now that everything is ready, we can declare the taxonomy in our config.toml file, by adding the following note:

[taxonomies]
author = "authors"
tag = "tags"
category = "categories"

On a personal note, I have manually added an entry in the menu, knowing that the viewing address of your taxonomy will be: http://[mon site]/authors/

So that's how, thanks to taxonomies, this blog became a multi-author blog.