2

I've been racking my brains trying to get my head around categories and entries, hopefully someone can help and show me the best way.

I have an entry page with product information and this product is assigned to multiple categories across multiple groups. I need to exclude the category 'preferential' and only show entries that belong to the category group 'offers'.

Here's what I've got so far:

{% set categories = craft.categories.relatedTo(entry) %}    
{% set excludeCat = craft.categories.slug('preferential').first() %}

{% set allIds = [] %}
{% for id in categories.ids() if id != excludeCat.id %}
    {% set allIds = allIds|merge([id]) %}
{% endfor %}

{% set entries = craft.entries.relatedTo(allIds) %}

{% for offer in entries %}                    
    <a href="{{ offer.url }}">{{ offer.title }}</a><br/>
{% endfor %}

I've tried variations of 'group', 'ids' and 'groupId' but no joy. This is my first Craft CMS build so still learning.

Thanks!

4

You can make use of 'not' to exclude items from your parameters. An example of how this would be implemented in the above use case would be as follows

{% set categories = craft.categories.slug( 'not preferential' ) %}
{% set entries = craft.entries.relatedTo( categories ) %}

If you need to specify category groups or entry sections then use

{% set categories = craft.categories.group( 'groupHandle' ).slug( 'not preferential' ) %}
{% set entries = craft.entries.section( 'sectionHandle' ).relatedTo( categories ) %}

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.