2

I have the following code:

  {staff_member_groups}
    <h2>{staff_member_groups:staff_group_heading}</h2>
    <ul class="staff_member_groups">
      {staff_member_groups:staff_group_members}
        <li class="{switch='odd|even'}">
          <img src="{staff_member_groups:staff_group_members:staff_portrait}">
          <h3>{staff_member_groups:staff_group_members:title}</h3>
          <p class="meta">
            {staff_member_groups:staff_group_members:staff_position}
          </p>
          <p>
            {staff_member_groups:staff_group_members:staff_titles}
          </p>
        </li>
      {/staff_member_groups:staff_group_members}
    </ul>
  {/staff_member_groups}

which renders correctly except for the fact that {switch='odd|even'} returns odd on every list item. Is there a way to get this functionality while looping through a relationship tag?

Edit: I should clarify: {staff_member_groups} is a grid field, with {staff_member_groups:staff_group_members} being the relationship field within the grid field.

Edit #2: I’ve found the solution (still new around here, SE won’t let me answer my own questions really quickly yet). {switch='odd|even'} needs to be namespaced so that it switches within the context of the relationship field, like so: {staff_member_groups:staff_group_members:switch='odd|even'}. Otherwise, {switch='odd|even'} is going by the larger context of the entries tag that surrounds this template.

For clarity, my final block of code looks like this:

  {staff_member_groups}
    <h2>{staff_member_groups:staff_group_heading}</h2>
    <ul class="staff_member_groups">
      {staff_member_groups:staff_group_members}
        <li class="{staff_member_groups:staff_group_members:switch='odd|even'}">
          <img src="{staff_member_groups:staff_group_members:staff_portrait}">
          <h3>{staff_member_groups:staff_group_members:title}</h3>
          <p class="meta">
            {staff_member_groups:staff_group_members:staff_position}
          </p>
          <p>
            {staff_member_groups:staff_group_members:staff_titles}
          </p>
        </li>
      {/staff_member_groups:staff_group_members}
    </ul>
  {/staff_member_groups}
2
  • 1
    Thanks for posting your solution. When you are able to, please post it as an answer and accept it. Oct 20, 2013 at 22:53
  • 1
    I sure will, @JeremyGimbel.
    – Alfonso
    Oct 20, 2013 at 22:56

1 Answer 1

4

I’ve found the solution: {switch='odd|even'} needs to be namespaced so that it switches within the context of the relationship field, like so:

{staff_member_groups:staff_group_members:switch='odd|even'}

Otherwise, {switch='odd|even'} is going by the larger context of the entries tag that surrounds this template.

For clarity, my final block of code looks like this:

  {staff_member_groups}
    <h2>{staff_member_groups:staff_group_heading}</h2>
    <ul class="staff_member_groups">
      {staff_member_groups:staff_group_members}
        <li class="{staff_member_groups:staff_group_members:switch='odd|even'}">
          <img src="{staff_member_groups:staff_group_members:staff_portrait}">
          <h3>{staff_member_groups:staff_group_members:title}</h3>
          <p class="meta">
            {staff_member_groups:staff_group_members:staff_position}
          </p>
          <p>
            {staff_member_groups:staff_group_members:staff_titles}
          </p>
        </li>
      {/staff_member_groups:staff_group_members}
    </ul>
  {/staff_member_groups}
1
  • Possibly worth noting that this applies to {count} as well.
    – grovberg
    Nov 11, 2013 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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