pix3l
March 5, 2020, 3:01pm
1
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
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
pix3l
March 5, 2020, 4:26pm
4
You mean like this?
I don’t see any “slug” field inside the json.
pix3l
March 5, 2020, 4:32pm
6
Well, there is no field “slug” appearing in my 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.
pix3l
March 14, 2020, 10:41am
10
Zignature:
{“slug”:true}
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.