1

I'd like to set a user defined order for a Categories field. For example, categories are:

Cat A Cat B Cat C

I'd like to give the ability for users to drag and drop the order, to say:

Cat B Cat A Cat C

What is the best way to handle this?

2 Answers 2

1

You can do this in the CP by default.

If you have a category field "categories", the user can select one or more categories and then drag them into the order they want.

On the frontend you just loop over them like this to show them in the order they selected.

{% for category in entry.categories %}
    <a href="{{ category.url }}">{{ category.title }}</a>
{% endfor %}

Same goes if you want to just show all categories from a category group, you can drag and drop them in the CP and then print them out in your code.

{% for category in craft.categories.group('cats') %}
    <a href="{{ category.url }}">{{ category.title }}</a>
{% endfor %}
2
  • This doesn't appear to be the case, unfortunately: craftcms.stackexchange.com/questions/19308/… More specifically: the comment about ordering Categories attached to another element does not represent an actual option within the Craft Admin. In the past, if this has been a requirement, I've just made the Category Group a Section, and used Entries as the relational object. As far as I can tell, there isn't a fundamental, functional, or performance difference between relating Entries via other Entries vs. via Categories. Commented Oct 17, 2017 at 21:10
  • Of course, Categories can by organized in their Category Group hierarchy view. Just make sure you've selected "Structure" from the sorting drop-down, and they'll be sortable! Commented Oct 17, 2017 at 21:13
0

I ended up finding this code snipped, which worked for me:

https://gist.github.com/zackspear/7cdd656f9bb34e210676

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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