Articles / How to generate a link to a single in Craft CMS
Oct 07. 2018 · by Helge Sverre

How to generate a link to a single in Craft CMS

I recently had to work on a website that had to be fully localized and was having trouble figuring out how to link to "single" sections

I usually hardcode links to singles for sites that only use one language, however, in this project the slugs were also localizable, which meant I had to have a reliable way to link to a page without it breaking cross-locale.

And here is what I did when I needed a link to a single that would work in any language.

Example
TWIG
{% set aboutPage = craft.entries.section("aboutPage").one() %}

<a href="{{ aboutPage.url }}">{{ aboutPage.title }}</a>

A convention I made for myself with singles, is to suffix them with "Page" to denote that they are a single page instance.

I hope this quick little post helped some poor soul.

Helge Sverre Hessevik Liseth

Book 1:1 help from me on CodeMentor, get expert advice and help with CraftCMS, Flutter or Laravel.

Expertise
  • Flutter
  • CraftCMS
  • PHP
  • VueJS
Book session