1

I'm trying to display a list of child structure links, but only from a specific parent. The template I'm working on isn't for the parent in question so I don't think I can use 'entry.'

My structure is set up like this

  • Parent 1
  • --Child One
  • --Child Two
  • --Child Three
  • Parent 2
  • --Child One
  • --Child Two

I only want to display the children of Parent 1. What I have so far is

<ul id="sub-menu">
{% set pages = craft.entries.section('visit').level('>1') %}
{% nav entry in pages %}
<li><a href="{{ entry.url }}">{{ entry.title }}</a>
    {% ifchildren %}
        <ul>
           {% children %}
        </ul>
     {% endifchildren %}
</li>
{% endnav %}
</ul>

Using this outputs all of the second level children instead of just the children from Parent 1. Is there any way to specify that the only children I want to display must come from Parent 1? Can I specify a slug somehow?

Any help much appreciated

1

yes you can!

You may search for a slug craft.entries.slug('slug-of-first-parent'), select the first result .one(), and then the children by .getChildren(). Now you can proceed as you already did and limit your result to only elements with a level greater than 1 .level('>1') and list.

{% set pages = craft.entries.slug('slug-of-first-parent').one().getChildren().level('>1') %}

I am not sure whether this is the best and most effective solution, but it should definitely do the job.

Tip: You can display information about an entry and the methods and variables available by dumping them either by using var_dump($variable) in php code or {{ d($variable) }} (by using this free plugin) in your twig template. This can help a lot when dealing with entries and finding children or parents.

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.