Blog overview

Craft CMS — Dynamic Title Formats

One of my favorite features in Craft CMS is the possibility to create dynamic titles. I think it's currently only possible to use this for entries, but that's fine. For a few months, we have been using entries for everything, including categories, and so on. Brandon mentioned in one of the GitHub Issues that it will all come together in the future within the Content menu item. I'll replace the entries menu item. However, we are here to talk about dynamic titles.

What are dynamic titles?

In craft, an entry needs at least a title. Most of the time, you, as the author, write something in the title field, and it's fine. But imagine you have a lot of content, and then in the future, the content grows and grows. Many authors work on the content, and sometimes you have content where you need a strict naming convention for titles or a certain structure for the title. You can do that all the time by hand, but that is not fail-proof. With dynamic titles, you have the possibility to make life much easier for your authors or just for yourself. Sometimes, you have nothing to write by hand, and sometimes, you pick just some related entries from which the title will be created.

How it works

Let's get away with this… We have some products. The product has a product category, a title, and an ID/SKU number. Now, when you create a product entry, you have to write this in the title field: Product Category - Product Title - SKU Number. It’s easy, you think, but is it? I think not. So here comes the dynamic title into play. Someone was thinking about how the title is structured. Suppose we need to get the three things from above, plus a namespace. Now our final title format looks like this: P - category - product title - SKU — yes, that looks fine. We have a P, which is the namespace and stands simply for Products. Second is the category title, third is the product title, and the last one is the SKU. But how does this become a dynamic title? Craft has a powerful control panel. You can put some Twig logic in many of the input fields to make things dynamic. In our case, we have three fields from which we create the title later. This looks like this: P - {productsCategory.one().title} - {productsTitle} - {productsId}.

This is the product entry type

Craft cms dynamic titles 01

This is our product entry

As you see, there is a Products ID field, which is our ID / SKU. Below that, we have our Product title field, which is just the product name. Below the title comes the Products Category field. From these three fields and the namespace, we build our title.

Craft cms dynamic titles 02

Now, our product overview looks like this:

Craft cms dynamic titles 03

Or for our product advantages, it looks like this:

Craft cms dynamic titles 04

You can see, that you have a clean title for each item. A positive side effect is that you have disjoined your long title into short pieces, and this gives you the possibility to change your entire title format in the future very quickly.

I'm a big fan of title generation like this and I/we use it more and more.

What do you think about it? Are there any use cases for this in the future? Give it a try; it's awesome!