Display entries that belong to a category I have a page that should show entries within a category, and then entries within children of that category. Basically, I need a way to loop through entries that have a level 1 category but not a level 2 category.

To give an example, suppose I've got some categories set up like this:

1.

— 1.1.

—— 1.1.1.

— 1.2.

2.

There's also one entry related to each category. I'd then use code like this:

{# Reference the category #}
{% set cat = craft.categories.title('1.').first() %}

{# Find related entries #}
{% set entries = craft.entries.section('mySection').relatedTo(cat).find() %}

{# Print them out #}
{% for entry in entries %}
    &lt;p&gt;{{ entry.title }}&lt;/p&gt;
{% endfor %}


This would output:

1.

1.1.

1.1.1.

1.2.

How can I find entries which are directly related to the category “1.”?
Categories are Craft's implementation of a taxonomy, which itself is the classification of things into groups and sub-groups. In this respect, I suppose it makes sense that if an entry is related to a sub-category, it inherits (it's also related to) its parent. That would explain why you're getting all entries in this way.

At the moment, the only way I can think of is to test what level the category/sub-category is.

Something like this would get you started for the time being. I don't really like this because it seems convoluted, so I'll put my thinking cap on and I may revise.

{% set category = craft.categories.group('myGroup').title('1.').first() %}
{% set entries = craft.entries.section('mySection').relatedTo(category).find() %}
{% set isThisLevel = false %}
{% for entry in entries %}

    {# Test if this entry is related to the correct category level #}
    {% for relatedCat in entry.catField %}
        {% if relatedCat.level == category.level %}
            {% set isThisLevel = true %}
        {% endif %}
    {% endfor %}

    {# Only output if it's this category level #}
    {% if isThisLevel %}
        {# Do whatever you want #}
    {% endif %}

{% endfor %}