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?

1 Answer 1

{% 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;

  • 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
    Commented Sep 12, 2020 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
    Commented Oct 3, 2020 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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