Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

The website I'm working on has different sections and each section different types of entries. I would like the types of entries to be shown in the URL. I have followed this tutorial to do so.

In a site example.com where we have:

  • A Section called: My Section
  • With Entry Types: Example Entry Type, Another Entry type.

Utilizing the model from the tutorial, we could structure the URL for My Section as follows:

/my-section/{type}/{slug}

Which would give us URLs like these:

  • example.com/my-section/exampleEntryType/new-entry-1
  • example.com/my-section/exampleEntryType/new-entry-2
  • example.com/my-section/anotherEntryType/new-entry-3
  • example.com/my-section/anotherEntryType/new-entry-4

Notice that the entry types are the only part of the URL camelCased. This is because Entry Types can only be configured to have a Name, and a Handle; and handles can NOT have dashes. This interferes with keeping URLs consistent as the rest of the site uses dashes instead of spaces

The problem I am trying to solve is: how do we get the Entry Types to stay consistent with the rest of the site?

Does anyone have any suggestion on how to deal with this? Is this a problem that should be addressed in Craft? Should I be taking another approach for this?

I had considered using categories but the way the system is setup, entry types seem more appropriate.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You could just set your "Entry URL Format" to:

my-section/{type.name}/{slug}

Your entry type names couldn't be as clean when composing entries in the CP as you'd need to set them to something like "example-entry-type", but it would work.


Now for the really clean way:

Install the Low Regex for Craft plugin.

Set "Entry URL Format" to:

my-section/{type.handle|regex('/(^|[a-z])([A-Z])/e', 'strtolower(strlen("\\1") ? "\\1-\\2" : "\\2")',(type.handle))}/{slug}

This will convert the entry type handle from camelCase to all lowercase with dashes as word delimiters. Set the entry type handles to be like "exampleEntryType" and they will become "example-entry-type" when in a slug.

share|improve this answer
    
Ahhh, I overlooked the usefulness of that plugin. I think I will go for that. It's a shame there's no native way of doing it but for now this is just as good. Thanks a lot Simon! –  cballenar Jul 7 at 0:07
    
No problem, glad it'll work for you! –  Simon Kuran Jul 7 at 2:04

You can change the slug for the entry type to be whatever you want it to be. The one generated by Craft with the -'s is a default, not a requirement. Also there is a config setting, slugWordSeparator to control the default word separator.

share|improve this answer
    
From what I'm seeing now, entry types (not each entry, but the group of entry types) only have a 'name' and a 'handle'. This can be seen in the following screenshot. dropbox.com/s/yirnzrc7dn7jfqn/… –  cballenar Jul 6 at 17:45
    
entry type's don't have slugs and the handle cannot have dashes, although the name can. –  Ben Croker Jul 7 at 10:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.