<p>You can use the <a href="http://buildwithcraft.com/docs/templating/craft.categories#level" rel="nofollow"><code>level</code></a> parameter of a Categories ElementCriteriaModel to get a list of nth-level categories, just to make your category search a bit more robust.</p>

<p>Then, you can use Twig's <code>without</code> filter to create the set of entries related to the primary category but <em>not</em> related to its child categories:</p>

<pre><code>{# Identify the primary category you want #}
{% set cat = craft.categories.title('1.').level(1).first() %}

{# Identify the [child] categories you don't want #}
{# set childCats = cat.children #}

{# Grab an array of entries related to the primary category #}
{% set entries = craft.entries.section('mySection').relatedTo(cat).find() %}

{# Get an array of entries related to the child categories #}
{% set excludedEntries = craft.entries.section('mySection').relatedTo(childCats).find() %}

{# Exclude the second set and loop over the resulting array #}
{% for entry in entries|without(excludedEntries) %}
    &lt;p&gt;{{ entry.title }}&lt;/p&gt;
{% endfor %}
</code></pre>

<p>This is still a bit janky, and you'd have to tweak it for performance if you were interested in more than one primary category at a time... but maybe it's a start.</p>

CC BY-SA 3.0
2015-02-03T17:32:08.607Z
2015-02-03T17:32:08.607Z
Michael Rog
<p>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.</p>

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

<p>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.</p>

<pre><code>{% 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 %}
</code></pre>

CC BY-SA 3.0
2014-06-20T01:16:16.710Z
This is how I thought it would work, however there is seemingly no way to assign a level 2 category to an entry without it inheriting the level 1 category.

Therefore, I can't loop through entries with a level 1 category, but not a level 2 category; the entries with a level 2 category inherently have a level 1 category and there seems to be no way to exclude them.
Angus Fretwell
CC BY-SA 3.0
2014-06-20T08:55:25.280Z
Ah right, I didn't totally understand the situation. I've revised my answer but I'm still not happy with it. I'll have a think. I've also submitted a modification to the question to clarify the problem.
Stuart Whitehead
CC BY-SA 3.0
2014-06-25T05:26:06.193Z
Yeah, sorry my question was so vague I didn't know how to word it. I actually came to the same solution as the one you've suggested. Works fine but it's a little gross.
Angus Fretwell
CC BY-SA 3.0
2014-06-19T09:39:49.633Z
2014-06-20T08:53:31.780Z
2014-06-20T08:53:31.780Z
Stuart Whitehead
<p>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.</p>

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

<p>1.</p>

<p>— 1.1.</p>

<p>—— 1.1.1.</p>

<p>— 1.2.</p>

<p>2.</p>

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

<pre><code>{# 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 %}
</code></pre>

<p>This would output:</p>

<p>1.</p>

<p>1.1.</p>

<p>1.1.1.</p>

<p>1.2.</p>

<p>How can I find entries which are <strong>directly</strong> related to the category “1.”?</p>

<templating><categories>
Display entries that belong to a category
# Display entries that belong to a category

(Asked by: username_0 on 2014-06-19)

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 %}
    {{ entry.title }}
{% 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.”?

**Tags:** templating, categories

---

## 2 answers

### Answer by username_2 on 2014-06-19. Score: 9 | Accepted answer

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 %}

> **Comments:**
> **username_0**: This is how I thought it would work, however there is seemingly no way to assign a level 2 category to an entry without it inheriting the level 1 category.  Therefore, I can't loop through entries with a level 1 category, but not a level 2 category; the entries with a level 2 category inherently have a level 1 category and there seems to be no way to exclude them.  
> **username_2**: Ah right, I didn't totally understand the situation. I've revised my answer but I'm still not happy with it. I'll have a think. I've also submitted a modification to the question to clarify the problem.  
> **username_0**: Yeah, sorry my question was so vague I didn't know how to word it. I actually came to the same solution as the one you've suggested. Works fine but it's a little gross.  

---

### Answer by username_1 on 2015-02-03. Score: 3

You can use the level parameter of a Categories ElementCriteriaModel to get a list of nth-level categories, just to make your category search a bit more robust.
Then, you can use Twig's without filter to create the set of entries related to the primary category but not related to its child categories:
{# Identify the primary category you want #}
{% set cat = craft.categories.title('1.').level(1).first() %}

{# Identify the [child] categories you don't want #}
{# set childCats = cat.children #}

{# Grab an array of entries related to the primary category #}
{% set entries = craft.entries.section('mySection').relatedTo(cat).find() %}

{# Get an array of entries related to the child categories #}
{% set excludedEntries = craft.entries.section('mySection').relatedTo(childCats).find() %}

{# Exclude the second set and loop over the resulting array #}
{% for entry in entries|without(excludedEntries) %}
    {{ entry.title }}
{% endfor %}

This is still a bit janky, and you'd have to tweak it for performance if you were interested in more than one primary category at a time... but maybe it's a start.


---

