1

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

1

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.

  • Thanks very much! – Matthias Sep 25 '20 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.