<p>Try this:</p>

<pre><code>&lt;li {% if entry.level == 1 and entry.children|length %}class="has-dropdown"{% endif %}&gt;
</code></pre>

CC BY-SA 3.0
2014-08-04T22:15:01.070Z
2014-08-04T22:15:01.070Z
Christian Steger
<p>The <a href="http://buildwithcraft.com/docs/templating/entrymodel#methods">Methods</a> section on the <a href="http://buildwithcraft.com/docs/templating/entrymodel">EntryModel</a> documentation page has a lot of useful things for working with navigation for Structure sections. You can use <a href="http://buildwithcraft.com/docs/templating/entrymodel#hasDescendants"><code>hasDescendants()</code></a>:</p>

<pre class="lang-twig prettyprint-override"><code>{% if entry.hasDescendants() %}class="has-dropdown"{% endif %}
</code></pre>

CC BY-SA 4.0
2019-07-01T17:44:05.277Z
Keep in mind, that this solution will even find child entries that are disabled, so you will have the class added even if there are only disabled pages within this part of the navigation tree.
Manuel
CC BY-SA 3.0
2014-08-05T00:20:54.883Z
2014-08-05T00:20:54.883Z
Simon Kuran
<p>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.</p>

<p>Current Code is:</p>

<pre><code>{% set entries = craft.entries.section('navigation') %}

&lt;ul&gt;
    {% nav entry in entries %}
        &lt;li&gt;

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

                {# Link to a custom url #}
                &lt;a href="{{ entry.customUrl }}"&gt;{{ entry.title }}&lt;/a&gt;

            {% else %}

                {# Link to a structure entry #}
                &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;

            {% endif %}

            {# Repeat this for child entries #}
            {% ifchildren %}
                &lt;ul class="dropdown"&gt;{% children %}&lt;/ul&gt;
            {% endifchildren %}

        &lt;/li&gt;
    {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>I want to add <code>class="has-dropdown"</code> to the first list element if it has children.</p>

<p>I have tried this:</p>

<pre><code>&lt;li {% ifchildren %}class="has-dropdown"{% endifchildren %}&gt;
</code></pre>

<p>But that does not work. How can this be achieved?</p>

<entries><navigation><structure>
Structure - Add class if element has children
# Structure - Add class if element has children

(Asked by: username_0 on 2014-08-04)

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 #}
                {{ entry.title }}

            {% else %}

                {# Link to a structure entry #}
                {{ entry.title }}

            {% endif %}

            {# Repeat this for child entries #}
            {% ifchildren %}
                {% children %}
            {% endifchildren %}

        
    {% endnav %}


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


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

**Tags:** entries, navigation, structure

---

## 2 answers

### Answer by username_2 on 2014-08-05. Score: 10 | Accepted answer

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

> **Comments:**
> **username_3**: Keep in mind, that this solution will even find child entries that are disabled, so you will have the class added even if there are only disabled pages within this part of the navigation tree.  

---

### Answer by username_1 on 2014-08-04. Score: 4

Try this:


---

