Forum

Thread tagged as: Question

List and detail - creating a unique slug

Because it's likely the title could be the same for more than one item, I'd like to create a unique slug. I thought I might be able to make a slug using the title and also the item ID, but it's not working. What am I doing wrong?

<perch:content id="slug" type="slug" for="_id" />
Simon Clay

Simon Clay 127 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The for fields need to exist in the form.

Simon Clay

Simon Clay 127 points

Ok, so I needed to add this to my template:

<perch:content id="_id" suppress="true" />
<perch:content id="slug" type="slug" for="title _id" suppress="true" />

Thanks Drew.

Simon Clay

Simon Clay 127 points

The problem with the above is that it creates a field in the form for the editor to see with the id number in. adding type="hidden" to the _id causes it not to be passed into the slug.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, I can see how that would be a problem.

Simon Clay

Simon Clay 127 points

Why if it's hidden in the form is it not passed to the slug?

Is there any way to get a unique slug?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Hidden items aren't included in the form, so they're not in the post.

It's something we need to look at, as I've indicated.

Simon Clay

Simon Clay 127 points

Ok, thanks very much Drew.

Greetings, I'm have the same trouble with duplicating titles. I'm just curious if the status on this has changed?

Many thanks! Nick

Yes, I'm having the same problem. I have a list of Job Postings that link to a detail page, but because the slug isn't unique (many job titles have the same name) it isn't working properly. There must be something unique about an entry that can be passed in a list/detail scenario, no?

Use a date field in combination with the job title to generate the slug?

From Perch documentation:

You can also use a space-separated list of IDs in the for attribute. This will draw content from multiple fields.

https://docs.grabaperch.com/docs/templates/attributes/type/slug/

I'm using multiple space-separated id's to generate the slog, but only the first id is being used in the created slug.

<li><a href="detail.php?s=<perch:content id="slug" type="slug" for="title landtrust" />"> <perch:content id="title" type="text" />, <perch:content id="landtrust" type="text" /></a></li>

Asking again: is there a way to generate a unique slug. I've tried using a space-separated list of ids, but that doesn't seem to be working. For example, in my template I have two fields:

<perch:content id="eventTitle" label="Title" title="true" /> <perch:content id="eventDateTime" format="F j, Y, ga" label="Date" type="date" time="true" />

and I would like to generate a unique slug by combining the two fields, so I have:

<perch:content id="slug" type="slug" for="eventTitle eventDateTime" />

Only the first field is used in the slug.

Anyone?

Order of the fields doesn't seem to matter, my code below skips the eventDateTime and only makes the eventTitle the slug, so field order doesn't matter. I tested it with another text field and it worked as expected.

Perhaps date fields aren't able to made into slugs...

<perch:content id="eventTitle" type="text" label="Title" required="true" title="true"  order="1"  />

<perch:content id="eventDateTime" type="date" label="Starts" format="d F Y H:i" fieldorder="mdy" order="4" time="true" divider-before="Date & Time" />

<perch:content id="eventSlug" type="slug" for="eventDateTime eventTitle"  />