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?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityI'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?
I ended up finding this code snipped, which worked for me:
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 %}