Generate URL from Titel

Hey,

is there a way or a php snippet, that would generate a field “url” from a possible Title field?
In a perfect world, this snippet or function would replace “spaces” with “-” and “&” with “and”.

eg:
Title: Sound & Music
URL: sound-and-music

Thanks in advance :slight_smile:

Add the slug option to the field.

{"slug":true}

it will unfortunately remove the &.

To replace the & you’ll need to use the BetterSlugs addon and create a callback function

You mean like this?
I don’t see any “slug” field inside the json.

Yes like that :slight_smile:

Well, there is no field “slug” appearing in my json:
slug-json

I added the field “url” by hand btw.

Then what am I doing wrong?

No there won’t be a field called slug. It’s Just a mechanism to retrieve an entry by using the slug that’s created in the retrieving URL.

If you need a field called slug, again, you should try the BetterSlugs add-on. It has some more options you may find handy.

Reisen & Natur will create the slug reisen-natur.

Reisen und Natur will create the slug reisen-und-natur.

In your front-end you need to configure to use that slug with a routing mechanism.

In your front-end the slug can be retrieved as data.title_slug.

Checkout a small tutorial about Nuxt.js with Cockpit cms.
Dynamic routing using the slug is explained in part 2: https://willbrowning.me/building-a-static-blog-with-nuxt-js-and-cockpit-headless-cms-part-2-dynamic-routes/

It seems _slug is appended to the fieldname and returned as fieldname_slug.

This actually worked as intended. Thank you so much.
My misstake: I had to go to the “title” field in every entry and re-write the title, in order to make this work, as Cockpit does not automatically set a slug for already existing title-entries.

Glad it works now :smiley: