4

I'm trying to output all the siblings of the current entry in a multi level structure like this:

{% for sibling in entry.siblings() %}
    <li>{{ sibling.link }}</li>
{% endfor %}

This works well, but how can I make this output all the siblings and the current entry?

I know I could add:

    <li>{{ entry.link }}</li>

before or after the sibling.link, but that would mess up the sort order.

If this can't be done this way, maybe I could somehow output the current branch/depth using a different approach? Any suggestions?

4

What about doing it like so:

{% set parent = entry.getParent() %}
{% set siblingsPlusMe = parent.getDescendants(1) %}

{% for entry in siblingsPlusMe %}
    <li>{{ entry.link }}</li>
{% endfor %}
3
  • Tested your code an it solves my problem. Thanks! In the meantime I also came up with a solution, see post below.
    – Alf Vestre
    Dec 5 '14 at 19:57
  • And I was so fast in answering this, @AlfVestre! Not sure if it is a good idea to use getChildren, I read somewhere in the Craft source codes that it was deprecated (works for now, I know!), so better use getParent().getDescendants(1), like in this example.
    – carlcs
    Dec 5 '14 at 21:37
  • Checked the the Craft source code (BaseElementModel.php) and you're absolutely right that getChildren is deprecated and should not be used. I have updated my code below based on this.
    – Alf Vestre
    Dec 5 '14 at 22:18
2

There are two possibilities. If the entry has a parent, then the entry+siblings will be all the children of that parent. If the entry does not have a parent (because it is a top level entry), then entry+siblings will be all the top level entries of the section.

{# define siblingsPlusMe so it is in scope outside of the if #}
{% set siblingsPlusMe = [] %}

{% if entry.level == 1 %} {# no parent #}
  {% set siblingsPlusMe = craft.entries.section(entry.section).level(1) %}

{% else %} {# parent #}
  {% set siblingsPlusMe = entry.parent.children %}

{% endif %}

{# now that it is set, we can use it #}
{% for sibling in siblingsPlusMe %}
  ...
{% end for %}
1

This will also work:

{% for sibling in entry.parent().children() %}
    <li>{{ sibling.link }}</li>
{% endfor %}

The code above uses deprecated Craft properties, use this instead:

{% for sibling in entry.getParent().getDescendants(1) %}
    <li>{{ sibling.link }}</li>
{% endfor %}
1

I ran into a problem trying to use the above solution, as getParent() wasn't returning anything (i think this may be because the entry url's i was trying to output where not children of another entry within the section?). Instead i did the following:

<ul>
  {% set sectionHandle = entry.getSection().handle %}
  {% set pages = craft.entries.section(sectionHandle).level(1) %}
    {% for page in pages %}
      <li>{{ page.getLink() }}</li>
    {% endfor %}
</ul>

I was following this tutorial: http://buildwithcraft.com/help/structure-nav but it was requiring me to specify a handle name for which section to use. I was trying to create a generic template for multiple sections, so wanted to get the section of the current entry dynamically.

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.