2

What I'm trying to do is to display child pages of a parent page. So far so good.

                {% if entry.parent %}
                    <div class="widget clearfix">
                        <h4 class="widgettitle"><a href="{{ entry.parent.url }}">{{ entry.parent.title }}</a></h4>
                        <ul class="clearfix">
                        {% set topic = entry.parent.id %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            <li class="cat-item"><a href="{{ subsection.url }}">{{ subsection.title }}</a></li>
                        {% endfor %}
                        </ul>
                    </div>
                {% endif %}

However if the sub page has another child page everything goes wrong as I see the results of the parent page that is a child itself.

How do I ignore the hierarchy and display the results of level 1 aka grand parent page / topic?

Thanks.

3

You can use the ancestors property (which is a shortcut to getAncestors()).

{% set targetEntry = entry %}
{% if entry.level > 1 %}
     {% set targetEntry = entry.ancestors.last %}
{% endif %}

To retrieve an ancestor with a specific level:

{% set targetEntry = entry %}
{% for ancestorEntry in entry.ancestors %}
    {% if ancestorEntry.level == "2" %}
        {% set targetEntry = ancestorEntry %}
    {% endif %}
{% endfor %}

And if you want a specific distance (i.e. 2 levels above), you can use the distance property.

{% set targetEntry = entry %}
{% if entry.level > 1 %}
    {% set targetEntry = entry.ancestors(2).last %}
{% endif %}
  • Thanks, works great on any of the sub-pages of the main ancestor/parent page, however when I visit the parent aka level 1 page nothing shows up :( – Mark H. Feb 1 '15 at 8:08
  • Yes — If it's a level 1 entry then it has no ancestors. You would want to check the level or entry.parent or entry.ancestors|length first. Updated answer with example. – Douglas McDonald Feb 1 '15 at 8:34
0

So, here is my final solution it looks ugly but works. I'm pretty sure that it could be done in a more effective / nicely looking way but this is my first website on Craft :)

                        {% set topic = entry.ancestors.first %}
                        {% if topic %}
                            <a href="{{ topic.url }}">{{ topic.title }}</a>
                        {% else %}
                            {{ entry.title }}
                        {% endif %}
                        </h4>
                        <ul class="clearfix">
                        {% if entry.level > 1 %}
                        {% for subsection in craft.entries.descendantOf(topic) if subsection.type != "state" %}
                            <li class="icon-list">
                                <a href="{{ subsection.url }}">{{ subsection.title }}</a>
                            {% if craft.request.segment(1) == subsection.slug %}
                                <span class="thumb-icon-list"><i class="fa fa-caret-right"></i></span>
                            {% endif %}
                            </li>
                        {% endfor %}

                        {% else %}

                        {% for subsection in craft.entries.descendantOf(entry.id) if subsection.type != "state" %}
                            <li class="icon-list">
                                <a href="{{ subsection.url }}">{{ subsection.title }}</a>
                            {% if craft.request.segment(1) == subsection.slug %}
                                <span class="thumb-icon-list"><i class="fa fa-caret-right"></i></span>
                            {% endif %}
                            </li>
                        {% endfor %}
                        {% endif %}

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.