<p>I have a scenario where I need category levels (4 levels) split in different sections of the page, also some categories have the same name, I explain:</p>
<ul>
<li>I need to list <strong>entries</strong> based on a <strong>category group</strong> called <strong>Stores</strong>.</li>
<li>Each <strong>Store</strong> has <strong>countries</strong> and each <strong>country</strong> has <strong>departments</strong> like movies, music, books and each <strong>department</strong> has <strong>genres</strong></li>
</ul>
<p>Exactly as you see here:</p>
<p><a href="https://i.sstatic.net/Miz8pl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Miz8pl.png" alt="4 Level Category" /></a></p>
<p>It is easy to display those categories like this: <a href="https://craftcms.com/docs/3.x/categories.html#example" rel="nofollow noreferrer">https://craftcms.com/docs/3.x/categories.html#example</a></p>
<p>but! the problem is: I need to split those category levels in different sections of the page, something like this:</p>
<p><a href="https://i.sstatic.net/ZHpfyl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZHpfyl.png" alt="Page" /></a></p>
<p>so for example when:</p>
<ol>
<li>I click on <strong>Barnes&amp;Noble</strong> I should get:</li>
</ol>
<ul>
<li>All the countries, in this case US and UK</li>
<li>All the departments (merged if the name is the same)</li>
<li>All the genres (merged if the name is the same)</li>
</ul>
<p>**Like this: (I mix the colors for you to understand that categories are merged)</p>
<p><em>to the merge, this is silly but I feel like I need something in the url like /barnesnoble/us+uk/movies/scifi (like &quot;hey! give all the departments and genres merged from all the countries.</em></p>
<p><a href="https://i.sstatic.net/nHErNl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/nHErNl.png" alt="enter image description here" /></a></p>
<ol start="2">
<li>If I click on <strong>US</strong> I should get:</li>
</ol>
<ul>
<li>Only the Departments and Genres from US like this (NOT UK of course).</li>
</ul>
<p><a href="https://i.sstatic.net/XXl9wl.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/XXl9wl.png" alt="enter image description here" /></a></p>
<p>So! I have this code (I try to split the categories but of course is not 100% working as I want).</p>
<pre><code>{% set storesStores = category.level == 1 ? category : category.ancestors.one() %}

{% set storesCountries = craft.categories()
    .descendantOf(storesStores)
    .level(2)
    .all() %}

{% set storesDepartments = craft.categories()
    .descendantOf(storesStores).level(2)
    .level(3)
    .all() %}

{% set storesGenres = craft.categories()
    .descendantOf(storesStores).level(3)
    .level(4)
    .all() %}

&lt;!-- Store Title --&gt;
&lt;h2&gt;
    &lt;a href=&quot;{{ storesStore.url }}&quot;&gt;{{ storesStore.title }}&lt;/a&gt;
&lt;/h2&gt;

&lt;!-- Countries --&gt;
{% for country in storesCountries %}
    &lt;a href=&quot;{{ country.url }}&quot;&gt;
        {{ country.title }}
    &lt;/a&gt;
{% endfor %}

&lt;!-- Departments --&gt;
{% for department in storesDepartments %}
    &lt;a href=&quot;{{ department.url }}&quot;&gt;
            {{ department.title }}
    &lt;/a&gt;
{% endfor %}

&lt;!-- Genres --&gt;
{% for genre in storesGenres %}
    &lt;a href=&quot;{{ genre.url }}&quot;&gt;
            {{ genre.title }}
    &lt;/a&gt;
{% endfor %}
</code></pre>
<p>and finally I list the entries added to those categories like this:</p>
<pre><code>{% set products = craft.entries()
    .relatedTo(category)
    .orderBy('releasesSpine')
    .all() %}

{% for product in products %}
   {{ product.title }}
{% endfor %}
</code></pre>
<p><em>You got the idea? Do I need to explain more? Let me know! I feel totally lost at this point, I read many times the docs but I can't find a solution, I even consider moving away from categories and using 4 dropdown fields, but Craft don't have conditionals yet and I don't know how list entries based on combinations of those dropdowns, I feel categories works better in this situation, or I'm wrong?</em></p>

CC BY-SA 4.0
2021-06-22T08:51:46.633Z
If it's not too late, I'd highly recommend revisiting your content model, as this setup will not serve you well for the future. You should split your different content types into smaller groups which tightly define what the content "IS", semantically speaking, instead of mashing them all together into a single generic category group. Stores, Countries, Departments and Genres would work better as 4 separate category groups (Stores could arguably be a section of entries). You would then use relationships to tie everything together.
James Smith
CC BY-SA 4.0
2021-06-22T17:48:39.353Z
Is not too late! (I'm open to any suggestion). So you suggest create 4 category groups: Stores, Countries, Departments and Genres and use Relationships to tie them, I'll try.

Just one thing: The only problem is, there will not be conditionals, by example: If a Store don't have UK, the user still can choose UK, same for the Department, or Genres, is correct?

(actually I I even thought use Dropdowns (field type) but the users wont be able to add more items, so thought in Sections w/ Structures), but I'll try with Categories first as you suggest.
user15280052
<entries><categories><filter><merge><layout>
Split categories by levels / List categories merged by name
# Split categories by levels / List categories merged by name

(Asked by: username_0 on 2021-06-21)

I have a scenario where I need category levels (4 levels) split in different sections of the page, also some categories have the same name, I explain:

I need to list entries based on a category group called Stores.
Each Store has countries and each country has departments like movies, music, books and each department has genres

Exactly as you see here:

It is easy to display those categories like this: https://craftcms.com/docs/3.x/categories.html#example
but! the problem is: I need to split those category levels in different sections of the page, something like this:

so for example when:

I click on Barnes&Noble I should get:


All the countries, in this case US and UK
All the departments (merged if the name is the same)
All the genres (merged if the name is the same)

**Like this: (I mix the colors for you to understand that categories are merged)
to the merge, this is silly but I feel like I need something in the url like /barnesnoble/us+uk/movies/scifi (like "hey! give all the departments and genres merged from all the countries.


If I click on US I should get:


Only the Departments and Genres from US like this (NOT UK of course).


So! I have this code (I try to split the categories but of course is not 100% working as I want).
{% set storesStores = category.level == 1 ? category : category.ancestors.one() %}

{% set storesCountries = craft.categories()
    .descendantOf(storesStores)
    .level(2)
    .all() %}

{% set storesDepartments = craft.categories()
    .descendantOf(storesStores).level(2)
    .level(3)
    .all() %}

{% set storesGenres = craft.categories()
    .descendantOf(storesStores).level(3)
    .level(4)
    .all() %}



    {{ storesStore.title }}



{% for country in storesCountries %}
    
        {{ country.title }}
    
{% endfor %}


{% for department in storesDepartments %}
    
            {{ department.title }}
    
{% endfor %}


{% for genre in storesGenres %}
    
            {{ genre.title }}
    
{% endfor %}

and finally I list the entries added to those categories like this:
{% set products = craft.entries()
    .relatedTo(category)
    .orderBy('releasesSpine')
    .all() %}

{% for product in products %}
   {{ product.title }}
{% endfor %}

You got the idea? Do I need to explain more? Let me know! I feel totally lost at this point, I read many times the docs but I can't find a solution, I even consider moving away from categories and using 4 dropdown fields, but Craft don't have conditionals yet and I don't know how list entries based on combinations of those dropdowns, I feel categories works better in this situation, or I'm wrong?

**Tags:** entries, categories, filter, merge, layout
> **Comments:**
> **username_1**: If it's not too late, I'd highly recommend revisiting your content model, as this setup will not serve you well for the future. You should split your different content types into smaller groups which tightly define what the content "IS", semantically speaking, instead of mashing them all together into a single generic category group. Stores, Countries, Departments and Genres would work better as 4 separate category groups (Stores could arguably be a section of entries). You would then use relationships to tie everything together.  
> **username_0**: Is not too late! (I'm open to any suggestion). So you suggest create 4 category groups: Stores, Countries, Departments and Genres and use Relationships to tie them, I'll try.  Just one thing: The only problem is, there will not be conditionals, by example: If a Store don't have UK, the user still can choose UK, same for the Department, or Genres, is correct?  (actually I I even thought use Dropdowns (field type) but the users wont be able to add more items, so thought in Sections w/ Structures), but I'll try with Categories first as you suggest.  

---

## 0 answers

