<p>I have worked it out using the following code. Not sure if I could make the conditional neater but this achieves what I need:</p>

<pre class="lang-twig prettyprint-override"><code>{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}
{% set parentId = entry.getParent().id %}
&lt;li class="nav--brand__item{% if parentId == 13 %} cs-g{% endif %}{% if parentId == 23 %} cs-k{% endif %}{% if parentId == 29 %} cs-h{% endif %}"&gt;
  ...
&lt;/li&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2014-09-22T12:45:17.660Z
2014-09-22T12:45:17.660Z
Gareth
<p>If you want to test whether <code>entry-with-id-X</code> is an ancestor of <code>entry</code>, do this (assuming x = 13):</p>

<pre><code>{% set entryWithIdX = craft.entries.id(13).first() %}
...
{% if entryWithIdX.isAncestorOf(entry) %} ... {% endif %}
{# or #}
{% if entry.isDescendantOf(entryWithIdX) %} ... {% endif %}
</code></pre>

<p><a href="http://buildwithcraft.com/docs/templating/entrymodel#isAncestorOf" rel="nofollow">isAncestorOf</a> and <a href="http://buildwithcraft.com/docs/templating/entrymodel#isDescendantOf" rel="nofollow">isDescendantOf</a> take an entry as parameter, not an entryId.</p>

<p>It would be a good idea to test to see whether <code>craft.entries.id(13)</code> returns anything before calling <code>first()</code> on it, but that's a separate issue.</p>

CC BY-SA 3.0
2014-09-22T11:05:09.623Z
Hi Marion, I am not sure I completely follow. I have pasted in what I now have which doesn't work.
Gareth
CC BY-SA 3.0
2014-09-22T07:13:17.960Z
2014-09-22T07:13:17.960Z
Marion Newlevant
<p>I have a structure section which is three levels deep and I want to add a class when the entry has a parent id of X. I am trying something like:</p>

<pre class="lang-twig prettyprint-override"><code>{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}
&lt;li class="nav--brand__item{% if entry.isAncestorOf(13) %} cs-g{% endif %}"&gt;
  ...
&lt;/li&gt;
{% endfor %}
</code></pre>

<p>This gives me a "Recoverable error":</p>

<blockquote>
  <p>Argument 1 passed to Craft\BaseElementModel::isAncestorOf() must be an
  instance of Craft\BaseElementModel, string given</p>
</blockquote>

<p>What am I doing wrong?</p>

<p>Updated code:</p>

<pre class="lang-twig prettyprint-override"><code>{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}
{% set entryWithId13 = craft.entries.id(13).first() %}
{% set entryWithId23 = craft.entries.id(23).first() %}
{% set entryWithId23 = craft.entries.id(29).first() %}
&lt;li class="nav--brand__item{% if entryWithId13.isAncestorOf(entry) %} cs-h{% endif %}{% if entryWithId23.isAncestorOf(entry) %} cs-k{% endif %}{% if entryWithId29.isAncestorOf(entry) %} cs-h{% endif %}"&gt;
...
&lt;/li&gt;
{% endfor %}
</code></pre>

<structure>
How do I set a class if entry is a child of a particular parent?
# How do I set a class if entry is a child of a particular parent?

(Asked by: username_0 on 2014-09-22)

I have a structure section which is three levels deep and I want to add a class when the entry has a parent id of X. I am trying something like:
{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}

  ...

{% endfor %}

This gives me a "Recoverable error":

Argument 1 passed to Craft\BaseElementModel::isAncestorOf() must be an
  instance of Craft\BaseElementModel, string given

What am I doing wrong?
Updated code:
{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}
{% set entryWithId13 = craft.entries.id(13).first() %}
{% set entryWithId23 = craft.entries.id(23).first() %}
{% set entryWithId23 = craft.entries.id(29).first() %}

...

{% endfor %}

**Tags:** structure


---

## 2 answers

### Answer by username_0 on 2014-09-22. Score: 1 | Accepted answer

I have worked it out using the following code. Not sure if I could make the conditional neater but this achieves what I need:
{% for entry in craft.entries.section('brand').descendantOf(entry.id).level(3) %}
{% set parentId = entry.getParent().id %}

  ...

{% endfor %}


---

### Answer by username_1 on 2014-09-22. Score: 2

If you want to test whether entry-with-id-X is an ancestor of entry, do this (assuming x = 13):
{% set entryWithIdX = craft.entries.id(13).first() %}
...
{% if entryWithIdX.isAncestorOf(entry) %} ... {% endif %}
{# or #}
{% if entry.isDescendantOf(entryWithIdX) %} ... {% endif %}

isAncestorOf and isDescendantOf take an entry as parameter, not an entryId.
It would be a good idea to test to see whether craft.entries.id(13) returns anything before calling first() on it, but that's a separate issue.

> **Comments:**
> **username_0**: Hi Marion, I am not sure I completely follow. I have pasted in what I now have which doesn't work.  

---

