Hello,
i want to display the year of the publishing date of a single blog post in the url to the post, but i don’t know how. I’m using the new Starterkit as reference.
Thanks!
Hello,
i want to display the year of the publishing date of a single blog post in the url to the post, but i don’t know how. I’m using the new Starterkit as reference.
Thanks!
What is your expected URL pattern?
Oh, sorry, forgot to mention.
I expect “example.com/year/article-title”
Ok, unless you actually put your articles into year folders, the way to achieve this would be to use routes in combination with a custom URL method in a page model that overwrites the default url()
method.
In fact, it would be great if only one pattern was defined for custom urls and integrated with the router
Are you able to provide some guidance on this?
I am trying to achieve this outcome.
Current url: /parent/child
Desired url: /authors-of-child/child
Where authors-of-child is just a field from the child page, stored as a tag(s). The “author” field usually contains only one name in the format 'FirstName LastName but occassionally will have more than one author.
Thanks in advance!
Does this mean if there are multiple authors, those authors should be stringed together?
Basically, you need two routes:
I’m assuming your parent page is called blog
.
'routes' => [
[
'pattern' => '(:any)/(:any)',
'action' => function ($author, $subpage) {
if ($page = page('blog')->find($subpage)) {
return $page;
}
$this->next();
}
],
[
'pattern' => 'blog/(:any)',
'action' => function ($slug) {
if ($page = page('blog/' . $slug)) {
$author = Str::slug(implode(' ', $page->authors()->split(',')));
go($author . '/' . $slug);
}
}
],
],
(not tested)
Ideally, you would also create a Page model that rewrites the URL to the target URL, so that the redirect is not necessary.