2

I'm trying to put a single category (as long as it's populated) at the end of an category list. My code is working so far, is there an easy way to pick out the 'Others' category and append it to the list?

{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories.relatedTo({ sourceElement:
    entryIds }).craft.categories.group('brands').find() %}

{% for category in categories %}
    {% set image = craft.assets.relatedTo(category).first() %}

    <a href="{{ category.url }}">
        {% if image|length %}
            <img src="{{ image.url }}" />
        {% else %}
            <img src="{{siteUrl}}assets/img/placeholder.jpg" />
        {% endif %}
    </a>
{% endfor %}

Thanks for any help, ready to learn something new.

2

Is there a reason why you don’t just drag the “Others” category to the end in the CP? The sort order will be the same for the front-end with your code:

{% set entryIds = craft.entries.section('toys').ids() %}
{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
}) %}

I corrected your code, you never append another .craft.categories criteria model to a criteria model. Have a look at the docs for the allowed syntax:

https://craftcms.com/docs/templating/elementcriteriamodel

Update:

Here’s example code that should work to list the categories alphabetically with the special “Others” category listed at the end.

{% set entryIds = craft.entries.section('toys').ids() %}

{% set categories = craft.categories({
    relatedTo: { sourceElement: entryIds },
    group: 'brands',
    order: 'title',
}) %}

{% set otherCategory = null %}

{% for category in categories %}
    {% if category.id == 72 %}
        {% set otherCategory = category %}
    {% else %}
        <a href="{{ category.url }}">{{ category.title }}</a>
    {% endif %}
{% endfor %}

{% if otherCategory %}
    <a href="{{ otherCategory.url }}">{{ otherCategory.title }}</a>
{% endif %}
  • If I drag it to the end will the rest remain alphabetical? – Kitsune Dec 6 '16 at 10:46
  • No, everything will be listed the same as in the CP. Do you want alphebetically sort order for the front-end and don’t wanna take care of it in the CP? – carlcs Dec 6 '16 at 10:47
  • Actually, both as I noticed choosing a category when creating a post is sort by structure and not title (which makes it easier to find something) – Kitsune Dec 6 '16 at 10:53
  • Yep, it’s sorted by structure but you can change it to title sort order in the CP. Anyways, I updated the answer to show you how to list alphabetically. Let me know if you’re having problems making it work for your needs. – carlcs Dec 6 '16 at 10:56
  • @Kitsune updated the code, using a dedicated params variable is a bit cleaner. – carlcs Dec 6 '16 at 11:03

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.