0

I've been scratching my head over this for awhile and I can't seem to solve it.

What I'm after is the following.

I have various categories with numerous sub categories. I'd like to be able to display the sub categories of a category that is related to an entry which is how my navigation is set up.

The code at the moment look like this...

{% set entries = craft.entries.find({ section: 'landingPages' }) %}
   {% for entry in entries %}
       <li><a href="{{ entry.url }}" class="{% set colour = entry.category.find() %}{% for category in colour %}{{ category.slug|striptags }}{% endfor %}" {% set colourTwo = entry.category.find() %}
           {% for category in colourTwo %} style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" {% endfor %}>{{ entry.title }}</a>
           <div class="sub {% set sub = entry.category.find() %}{% for category in sub %}{{ category.slug|striptags }}{% endfor %}">
           {% set drop = entry.category.find({ }) %}
               <ul>
               {% for category in drop %}
                  <li><a href="{{ category.url }}" title="{{ category.title }}">{{ category.title }}</a></li>
               {% endfor %}
               </ul>
           </div>
        </li>
    {% endfor %}

At the moment it's just generating the category which is selected within the entry, i've tried various variations on descendant commands and nothing seems to work.

Any help would be greatly appreciated.

  • I am not exactly sure what you are trying to do. Are you trying to get the children of the related category in the natural category heirarchy? or are you trying to get descendent categories that are also assigned to the entry? – Douglas McDonald Oct 30 '14 at 5:29
  • 1
    You also seem to be doing a lot of extra work here that is not needed. If your initial entry only has one category assigned (which I assume from the above code that it does) then you can simply use: {% set category = entry.category.first %} and then access all of your category fields using {{ category.slug }}, {{ category.title }},{{ category.colour }}, etc. etc. You do not need to loop through categories every time you want to grab a field value. – Douglas McDonald Oct 30 '14 at 5:30
  • Cheers Douglas. Yeah i'm not the most efficient in my twigging. And yes i'm trying to get the children on the related category. – Dominic Leonard Oct 30 '14 at 9:05
6

Check out the documentation for craft.categories which should do what you need. In this example, I assume that 'landing pages' are the same as your level 1 categories, in which case you don't need to deal with the entries at all:

{% set categories = craft.categories.group('navCategories') %}

<ul id="nav">
    {# using 'nav' and 'children' will create a recursive loop #}
    {% nav category in categories %}
        <li>
            {% if category.level == 1 %}
                <a href="{{ entry.url }}" class="{{ category.slug }}" style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" >{{ category.title }}</a>
            {% else %}
                <a href="{{ entry.url }}" class="{{ category.slug }}">{{ category.title }}</a>
            {% endif %}
            {% ifchildren %}
                <div class="sub{{ category.level }} {{ category.slug }}">
                    <ul>
                        {% children %}
                    </ul>
                </div>
            {% endifchildren %}
        </li>
    {% endnav %}
</ul>

If you only want a certain number of levels then you can use a level filter. I.e:

{% set categories = craft.categories.group('navCategories').level('< 3') %}

Update: If level 1 categories are not the same as your landing pages then you can access the category through entry.category.first, and then category decendants using: {% for category in category.descendants %}...{% endfor %} or {% nav category in category.descendants %}...{% endnav %} using the recursive method above.

<ul id="nav">
    {% set entries = craft.entries.section('landingPages') %}
    {% for entry in entries %}
        {% set category = entry.category.first %}
        <li>
            <a href="{{ entry.url }}" class="{{ category.slug }}" style="border-color: {{ category.supplierColour }}; color:{{ category.supplierColour }};" >{{ entry.title }}</a>
            <ul class="sub {{ category.slug }}">
                {# using 'nav' and 'children' will create a recursive loop, if more levels are needed #}
                {% nav category in category.descendants %}
                    <li>
                        <a href="{{ category.url }}" title="{{ category.title }}">{{ category.title }}</a>
                        {% ifchildren %}
                            <ul class="sub{{ category.level }}">
                                {% children %}
                            </ul>
                        {% endifchildren %}
                    </li>
                {% endnav %}
            </ul>
        </li>
    {% endfor %}
</ul>

As a side-note: Keep in mind that you can also define css dynamically within the loop rather than defining it on the elements directly, if you wanted to.

{% set css %}
    a.{{ category.slug }} { 
        border-color: {{ category.supplierColour }}; 
        color:{{ category.supplierColour }}; 
    } 
{% endset %}
{% includeCss css %}
  • If I was just looking for categories that would be easy. The issue is there are "Landing Pages" which act as a window into each category. The Landing Page is then associated with a category which pulls in things like the category logo, colour. What I need is for the navigation to list out the entries in the "Landing Page" section, then find which category they are associated with... and then list out all children of that category. Hope that makes sense. Here is the URL to the development site. cicadaretail.neondigital.com – Dominic Leonard Oct 30 '14 at 15:48
  • That's why I mentioned the second part. I figured that you could put the two together yourself. But I have updated the answer with a full example. – Douglas McDonald Oct 30 '14 at 16:37
  • Another thing to mention is that categories can have their own url and template files (see the Docs link @Douglas provided). In this case it could make sense to delete the "Landing Pages" section altogether and instead use the category group not only for the navigation (first part of this nice answer) but also for the "Landing Pages" content. – carlcs Oct 30 '14 at 16:46
  • Structures and Entry Types also work great for this as well, which you can also build nav from. – Douglas McDonald Oct 30 '14 at 16:57

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.