8

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}
  • 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 In Jun 20 '14 at 18:46
  • Just tried that, no difference unfortunately. – Sean O'Grady Jun 20 '14 at 18:48
  • It seems that you want both of the following to load the entry template: example.com/cat1/subcat1/entry & 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? – Sean 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
17

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}
    
  • 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 9: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 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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