Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a structure section which I am using to create a navigation menu. It works ok and produces the list etc. I want to add a class to the li element if it has children.

Current Code is:

{% set entries = craft.entries.section('navigation') %}

    {% nav entry in entries %}

            {# Check for entry type / get related entry #}
            {% if entry.type == 'customLink' %}

                {# Link to a custom url #}
                <a href="{{ entry.customUrl }}">{{ entry.title }}</a>

            {% else %}

                {# Link to a structure entry #}
                <a href="{{ entry.url }}">{{ entry.title }}</a>

            {% endif %}

            {# Repeat this for child entries #}
            {% ifchildren %}
                <ul class="dropdown">{% children %}</ul>
            {% endifchildren %}

    {% endnav %}

I want to add class="has-dropdown" to the first list element if it has children.

I have tried this:

<li {% ifchildren %}class="has-dropdown"{% endifchildren %}>

But that does not work. How can this be achieved?

share|improve this question
up vote 5 down vote accepted

The Methods section on the EntryModel documentation page has a lot of useful things for working with navigation for Structure sections. You can use hasDescendants():

{% if entry.hasDescendants() %}class="has-dropdown"{% endif %}
share|improve this answer

Try this:

<li {% if entry.level == 1 and entry.children|length %}class="has-dropdown"{% endif %}>
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.