Assigning one or more people to pages like posts or projects is a common use case and Kirby's user system in combination with the users field is a perfect fit for this.
Authors can be created as Kirby users. A Kirby user can have one of different roles, and you can store any information in a user's profile, including a profile picture.
Before you add new users as authors, think about what information you want to store for these users. We can then set out to create a blueprint for our authors.
In this example, let's create an
author.yml blueprint, which we store in
/site/blueprints/users. In our user blueprint, let's define some fields:
Since name and email are default fields, we don't have to define them here. With this blueprint, we now have defined a new user role and can start adding authors with this role.
In the Panel, this setup will now look like this:
This can be extended to a full database of author contact details and more, depending on your use case.
Now that we have created a blueprint for the user and added some users in the Panel, we need a way to select one or more authors in our posts. Kirby has a users field that comes in handy for this purpose. Let's set it up in our
In this example we have defined a sidebar with some meta data fields and an authors field. The authors field has some default settings and options you can find in the users field docs. Here we have kept it simple and only set the
multiple option to
false, because we only want to select a single author.
In the content file
article.txt, the selected user is stored in
Note that since Kirby 3.3.0 Kirby stores the user ID instead of the user email so that the reference to the user is persistent even if the email changes. The user ID is a random string (folder name that contains the user related files) assigned to a user when the user is created.
You can now fetch the author data in the article template. This is just a basic example of how the article template could look:
This is very extendible and can give your articles a nice personal touch.
You can also build a list of all authors that have created an article and fetch their articles:
Create a new folder
authors with an
authors.txt file and an
In that template you can fetch all authors from the articles and build a list out of it.
The above code assumes that your articles are children of the
blog folder. If your article parent is called differently, adapt the page name accordingly.
As an alternative, you can put this list of authors with their articles into the blog template instead of using a separate authors page. In that case, fetch the articles from the current page instead.
Each author will show up on http://yourdomain.com/authors with the user avatar and a list of articles they have written. This can be extended with more info about each author or more info about each article.