I want to be able to get the slug of my news section dynamically by searching for its handle. Slugs tend to change during the development phase, but the handle doesn't. That's why I want to hard code the news handle in my template to refer to the news section from any other page.

How to do such a query?

{% set newsSlug = craft.entries().section('news').select('elements_sites.slug').one().slug ?? null %}

news - handle your section news;

select('elements_sites.slug') - for get only slug your section;

?? null - in case if there is no such section;

| improve this answer | |
  • Thanks, trying this on the section handle "articlesRelatedToArtist" returns "articles-related-to-artist", but the URI format is "artist-news/{slug}", so I actually need the query to return "artist-news" – Matthias Sep 12 at 18:07
  • I tried this query on the "news" section with slug 'news' but it returns the slug of the first news article.... I need it to return the 'news' slug, how do I do that? – Matthias Oct 3 at 14:36

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.