Answers.Body: <p>Try this:</p>
  
  <pre><code>&lt;li {% if entry.level == 1 and entry.children|length %}class="has-dropdown"{% endif %}&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-04T22:15:01.070Z
Answers.LastActivityDate: 2014-08-04T22:15:01.070Z
Answers.UserDisplayName: Christian Steger
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-01T17:44:05.277Z
Answers.Text: 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.
Answers.UserDisplayName: Manuel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-05T00:20:54.883Z
Answers.LastActivityDate: 2014-08-05T00:20:54.883Z
Answers.UserDisplayName: Simon Kuran
Body: <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>
  
Tags: <entries><navigation><structure>
Title: Structure - Add class if element has children
ThreadText: # 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') %}
  
  <ul>
      {% nav entry in entries %}
          <li>
  
              {# 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 %}
  
          </li>
      {% endnav %}
  </ul>
  
  ```
  
  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?
  
  **Tags:** entries, navigation, structure
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2014-08-05. Score: 10 | Accepted answer
  
  The [Methods](http://buildwithcraft.com/docs/templating/entrymodel#methods) section on the [EntryModel](http://buildwithcraft.com/docs/templating/entrymodel) documentation page has a lot of useful things for working with navigation for Structure sections. You can use [`hasDescendants()`](http://buildwithcraft.com/docs/templating/entrymodel#hasDescendants):
  
  ```twig
  {% 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:
  
  ```
  <li {% if entry.level == 1 and entry.children|length %}class="has-dropdown"{% endif %}>
  
  ```
  
  
  ---
  
  