2

I've got a section called 'web development' and there are four categories that apply to it. Three can be grouped together as 'original content' and one is a link list to external third party content.

I'd like to split my entry archive list such that one group lists all entries in the Web Development section that don't belong to the 'external' category (there will only ever be one 'external' category, but I may make new categories that apply to 'original content' so I don't want to hard-code the categories).

After that I'd like to loop through again but only listing entries that do belong to that 'external' category. For each entry I'd like to spit out which category it belongs to as well as the entry title and date.

I can't wrap my head around how to do this, I'm not sure I'm groking how to use relations. Or would I be better to create nested categories rather than trying to group them 'after the fact'?

EDIT: went with making categories and sub categories, but still can't get things to work!

{% set originalContentCategory   = craft.categories.slug('original-content').find() %}
{% set originalContentCategories = craft.categories.group('global').descendantOf(originalContentCategory).find() %}
{% for category in originalContentCategories %}
    <h3>{{ category.title }}</h3>
    {{ category.synopsis }}
{% endfor %}

This is listing all categories in the group, including the 'original-content' category itself. What am I doing wrong?

1
  • 1
    Replace find() with first() to save one category in your originalContentCategory var instead of an array.
    – carlcs
    Commented Aug 19, 2014 at 22:10

1 Answer 1

4

I would use the without filter and relations for this.

{# starting points #}
{% set externalCategory = craft.categories.slug('external-content').first() %}
{% set allEntries = craft.entries.section('webdev').find() %}

{# create the arrays/objects you need #}
{% set externalEntries = craft.entries.section('webdev').relatedTo(externalCategory).find() %}
{% set otherEntries = allEntries|without(externalEntries) %}

then use the usual for loops for each array / object you have created. That way, you only need to define your external-content category, all the rest is automatic, regardless of the number of other categories you have.

Depending on the number of entries you have and the kind of traffic you get, you might want to consider caching this.

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.