5

I have a gallery which is categorised. The gallery is a Structure section where the user adds an entry and then assigns it to a category - all good.

At the moment, when clicking on an entry title the URL is mysite.com/gallery/entry-slug but I would like it to be mysite.com/gallery/category-name/entry-slug.

I've looked around the Docs and StackExchange for a simple answer (assuming it would just be the addition of a tag) but the solutions seem very convoluted and way outside my grasp of understanding.

Any help appreciated

Thanks

6

A Categories field can be related to several categories. So you'll either need to limit your Category field to a single Category relationship or you'll need to make some assumption in how you build your URL as to which category will appear in the URL. The simplest assumption to make will be that you will always return the first category for your URL.

Entry URL Format in the Section Settings:

gallery/{categories.one().slug}/{slug}

Entry Page URL:

http://mysite.com/gallery/category-slug/entry-slug

In the Entry URL Format setting above, you are telling your slug to return the slug of the first item in your array of categories using this line: {categories.first().slug} where categories is the handle of your Categories Relations field.

You may also want to ensure your Categories Relations field is required.

  • For Craft 3, the Entry URL Format should use the new ElementQuery method .one(), as: gallery/{categories.one().slug}/{slug} – August Miller Nov 6 '18 at 22:00
  • Updated to Craft 3 syntax. We'll let your comment serve as the Craft 2 syntax example! – Ben Parizek Nov 7 '18 at 6:12
1

Should be I think:

Entry URL Format in the Section Settings:
gallery/{category.first.slug}/{slug}

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.