I have several users who write content. How can I do to display their names in templates ?
Thank’s for your help.
//get the users of the site
$users = $site->users();
foreach ($users as $user) {
echo $user->lastname() . $user->firstname();
}
For more options, see the Cheatsheet http://getkirby.com/docs/cheatsheet#user
If you want to get only users with a specific role, you can limit this by using one of the options in the Cheatsheet, e.g. to filter users with role “editor”:
$users = $site->users()->filterBy('role', 'editor');
I misspoke.
I want to display for each content, the name of the writer who wrote the content.
Sorry for my bad english…
Do you have an author field in your content file?
No ! Is it the only solution ?
The author’s name can not be displayed automatically, wihtout used an “author field” in the bueprint of content ?
No, I dont think so. If the editor of a content file is not saved anywhere, it cannot be displayed.
The account file for each user contains a history, but I think that is not permanent.
Maybe you can use a panel hook, which saves the current user to an author field of the content file automatically when the page is created or edited.
No, because Kirby does not store who edited which page to keep the content flexible.
You can use the user
field and then access the data by using $site->users()->find($page->author())
.
Here is an example hook that is triggered on page creation
kirby()->hook('panel.page.create', function($page) {
$user = site()->user();
try {
$page->update(array(
'author' => $user,
));
} catch(Exception $e) {
echo $e->getMessage();
}
});
You could also trigger on page update, if you want.
If you don’t want to use a hook, you could go with an author field of type “user” as @lukasbestle suggested. The current user is preselected, so the editor wouldn’t even have to enter/select anything. You could even make it readonly, so that users can see but not change the content of that field.
editor:
label: Editor
type: user
readonly: true