Is it possible to have a URI like this: /artist/{slug}/news to create an overview of all news articles of this particular artist?
I tried to set it up with Routes and with a section Entry URI, but I get a 404
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIs it possible to have a URI like this: /artist/{slug}/news to create an overview of all news articles of this particular artist?
I tried to set it up with Routes and with a section Entry URI, but I get a 404
You can achieve this by writing a module and using the UrlManager::EVENT_REGISTER_SITE_URL_RULES
event.
Basic example:
<?php
use craft\elements\Entry;
use craft\events\RegisterUrlRulesEvent;
use craft\web\UrlManager;
use yii\base\Event;
class CustomModule
{
public function init()
{
parent::init();
Event::on(
UrlManager::class,
UrlManager::EVENT_REGISTER_SITE_URL_RULES,
static function (RegisterUrlRulesEvent $event) {
$artistEntries = Entry::find()->section('artists')->all();
foreach ($artistEntries as $artistEntry) {
$event->rules[$artistEntry->uri] = ['template' => '_pages/artists/news'];
}
}
);
}
}
Then, in the _pages/artists/news.twig template, you'll need to extract the artist's slug from craft.app.request.url
and use it to get the Entry corresponding to your artist.