# Generate URL from Titel

**URL:** https://discourse.getcockpit.com/t/generate-url-from-titel/1248
**Category:** General
**Created:** [March 5, 2020, 3:01pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248 "2020-03-05T15:01:07Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![pix3l](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@pix3l](https://discourse.getcockpit.com/u/pix3l)
#### Post date: [March 5, 2020, 3:01pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/1 "2020-03-05T15:01:07Z")

</div>

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 🙂

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 3:09pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/2 "2020-03-05T15:09:11Z")

</div>

Add the slug option to the field.

`{"slug":true}`

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 3:11pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/3 "2020-03-05T15:11:32Z")

</div>

it will unfortunately remove the &.

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

---

<div class="post-metadata">

### Author: ![pix3l](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@pix3l](https://discourse.getcockpit.com/u/pix3l)
#### Post date: [March 5, 2020, 4:26pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/4 "2020-03-05T16:26:37Z")

</div>

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

 ![slug](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/001de5d5bf3f68609e3db7aaa866dae8be5d662e.png)

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 4:28pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/5 "2020-03-05T16:28:38Z")

</div>

Yes like that 🙂

---

<div class="post-metadata">

### Author: ![pix3l](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@pix3l](https://discourse.getcockpit.com/u/pix3l)
#### Post date: [March 5, 2020, 4:32pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/6 "2020-03-05T16:32:34Z")

</div>

Well, there is no field “slug” appearing in my json:  
 ![slug-json](https://canada1.discourse-cdn.com/flex030/uploads/getcockpit/original/1X/4aab0275944e18df276d9463924c721e61a9cccd.png)

I added the field “url” by hand btw.

Then what am I doing wrong?

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 5:50pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/7 "2020-03-05T17:50:57Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 5:57pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/8 "2020-03-05T17:57:19Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 5, 2020, 6:03pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/9 "2020-03-05T18:03:13Z")

</div>

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/](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.

---

<div class="post-metadata">

### Author: ![pix3l](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@pix3l](https://discourse.getcockpit.com/u/pix3l)
#### Post date: [March 14, 2020, 10:41am UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/10 "2020-03-14T10:41:47Z")

</div>

> [@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.

---

<div class="post-metadata">

### Author: ![Zignature](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zignature](https://discourse.getcockpit.com/u/Zignature)
#### Post date: [March 14, 2020, 12:49pm UTC](https://discourse.getcockpit.com/t/generate-url-from-titel/1248/11 "2020-03-14T12:49:21Z")

</div>

Glad it works now 😃
