9

I'm trying to mimic the URL structure exactly how I have it in my old CMS. If I select an entry to have both a category and subcategory I would like to have the subcategory in the URL along with the category and the entry slug.

For Example

http://example.com/cat1/subcat1/entry

Right now I have the Entry URL Format set to be {category.first().slug}/{slug}, but that only shows the first category. How do I access that optional subcat1 subcategory in that field?

EDIT

I have tried the following but it doesn't work

{category.first.slug }/{ category.first.children.first.slug ? category.first.children.first.slug ~ '/' : '' }{slug}

I have also tried

{category[0].slug}/{ category[1] ? category[1].slug ~ '/': ''}{slug}
Seán O'Grady
  • 1,582
  • 1
  • 14
  • 25
  • Can you try and use double curly braces {{ .. }} , if I remember correctly there was a change in Craft 2.0 which makes them necessary. – Victor Jun 20 '14 at 18:46
  • Just tried that, no difference unfortunately. – Seán O'Grady Jun 20 '14 at 18:48
  • It seems that you want both of the following to load the entry template:

    http://example.com/cat1/subcat1/entry & http://example.com/cat1/entry

    Is this correct?

    Probably you will need to use custom routes, and a bit of template logic to determine if a request segment is a category url or an entry url.

    I've not run into a need for segment 1 as a category url, it has always been something like /catalog/cat/entry or news/cat/entry/ Is there a specific reason you are not doing this way?

    – Steve Adams Jun 20 '14 at 20:01
  • Because the CMS I used before – Wordpress – structures posts with subcategories in the URL in this way. To be honest it makes the most sense, otherwise what is the point of subcategories in the first place? – Seán O'Grady Jun 20 '14 at 20:03
  • So .com/cat1/ would list all entries in cat1 including its subs, and .com/cat1/sub1/ would list only sub1 entries? Then you have .com/cat1/entryA/ and .com/cat1/sub1/entryA/ ? Craft is so flexible, you can make it behave however you need. It will help if you update your question with more example urls and what you expect to happen with each one. :) – Steve Adams Jun 20 '14 at 20:11
  • It is a bit strange I agree, but the category urls I have set to example.com/category/<category-name>/ The category slugs in the entry URL are just for contextual tips for the user & for SEO purposes... Anyways the question is how, not why :) – Seán O'Grady Jun 20 '14 at 21:03

1 Answers1

18

If you go into your Category Group’s settings and set the following settings:

  • Craft 2
    • Top Level URL Format: {slug}
    • Nested URL Format: {parent.uri}/{slug}
  • Craft 3
    • Category URI Format: {parent.uri}/{slug}

then you would be able to set your section’s Entry URL Format to this:

  • Craft 2

    {myCategoryField.last.uri}/{slug}
    
  • Craft 3

    {myCategoryField.inReverse().one().uri}/{slug}
    
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
  • This is handy. Is there an easy way to just output just the child category and not include the parent? – shorn Jul 10 '14 at 09:33
  • @shorn Not sure I’m following. Maybe post as its own question with more details? – Brandon Kelly Jul 10 '14 at 13:14
  • Ok, will post up a question shortly. – shorn Jul 10 '14 at 17:05
  • 1
    In regards to the Category URI Format, if you have a prefix before all category URI's, the example given here for Craft 3 results in the prefix being duplicated. In this case the following solution worked for me: prefix/{parent.slug}/{slug} (using parent.slug rather than parent.uri). – Mike Jun 18 '19 at 16:11
  • @Mike - The problem with your solution prefix/{parent.slug}/{slug} is, that its only for 2 levels. The gandparent and the grand-grand-grandparent are not sluggified with this method. Do u know what to do in this case? – Slowwie Aug 16 '20 at 18:42
  • I dont really understand what this section´s Entry URL Format is. After I changed the Category URI Format of the Category Group - I can get now 2 level URI's. Fine but what if my customer is nesting more levels. What do u mean with section's Entry URL format? As I can see there is no section for Categories... – Slowwie Aug 17 '20 at 05:05
  • @Slowwie This question is asking how to set entries’ URLs based on their selected categories. If you are just concerned with setting category URLs, you can safely ignore the part about sections (which contain entries). The category URL format above will work no matter how many levels you have. – Brandon Kelly Aug 17 '20 at 12:19