I have a channel called fruit. There is a custom field called 'parent' of type 'relationship' in this channel. The relationship is set to channel fruit.

When I edit my entries in this channel, I can set the field called parent to any of my other entries.

Example entries...

  • Orange
  • Banana
  • Apple
  • Pear (parent is Grape)
  • Grape
  • Lemon (parent is Banana)
  • Strawberry (parent is Apple)
  • Kiwi (parent is Grape)
  • Mango (parent is Apple)

Based on the above, with my setting of some entries to be parents of other entries, a list would appear as...

  • Apple
    • Strawberry
    • Mango
  • Banana
    • Lemon
  • Grape
    • Pear
    • Kiwi
  • Orange

Now I want to setup an ordered list to display this relationship dynamically so that the generated code results in...

<ul>
  <li>Apple
    <ul>
      <li>Strawberry</li>
      <li>Mango</li>
    </ul>
  </li>
  <li>Banana
    <ul>
      <li>Lemon</li>
    </ul>
  </li>
  <li>Grape
    <ul>
      <li>Pear</li>
      <li> Kiwi</li>
    </ul>
  </li>
  <li>Orange</li>
</ul>

I have been reading about the new way EE handles parent-child-sibling relationships in the Docs. But I am having problems coding the above.

Cheers

  • I guess you can't use 'parent' as name of a field. – Robson Sobral Aug 9 '13 at 13:52
  • I didn't use the word parent. I used 'site_parent'. I could have named it anything though. – DevServe Aug 9 '13 at 19:01

Not tested, but this should work. We're checking to see if each fruit has parents, and if it doesn't, then we display it (and any child fruits). It would be helpful if the fieldtype had something like Playa's :total_parents and :total_children tags, but it doesn't (which is one of the many reasons I still use Playa).

<ul>
{if '{parents field="child_fruit"}{title}{/parents}' == ''}
    <li>
        {title}
        {child_fruit}
        {if child_fruit:count == "1"}<ul>{/if}
            <li>{title}</li>
        {if child_fruit:count == child_fruit:count:total_results}</ul>{/if}
        {/child_fruit}
    </li>
{/if}
</ul>

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.