• Resolved codimex

    (@codimex)


    Hi, I don’t know if this has been answered before, but I can’t find it out there. I have this:

    My terms: <Taxonomy my_taxonomy><a href="{Field url}"><Term title /></a></Taxonomy>

    The output is nice enough:

    My terms: X Y Z

    However, I’d like to know if there’s a chance to get these two variations:

    1) Adding “and” before the last element:

    My terms: X Y and Z

    2) Adding commas and “and” before the last element:

    My terms: X, Y and Z

    Bonus: That’s an example of taxonomy listing. Is is also possible to achieve those when listing elements of a checkbox field? Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Eliot Akira

    (@miyarakira)

    Hello – It was a bit tricky to figure out, but the following should create a comma-separated list like you described.

    <Taxonomy category>
      <If count more_than value=1><If last> and </If></If>
      <a href="{Term url}"><Term title /></a><If not last>, </If>
    </Taxonomy>

    It uses If last and If not last to show , and and.

    Also, it uses If count to make sure it doesn’t display and when there’s only one term.

    Similarly for the checkbox field, replacing Taxonomy with Loop, and Term with Field.

    <Loop acf_checkbox=field_name>
      <If count more_than value=1><If last> and </If></If>
      <a href="{Field url}"><Field title /></a><If not last>, </If>
    </Loop>

    This assumes you’re using Advanced Custom Fields for the checkbox field. Otherwise, it might work with Loop field=field_name, but it depends on how the data is saved by the plugin that provides the custom field.

    If you have any further questions, please feel free to visit the discussion forum.

    Thread Starter codimex

    (@codimex)

    Thank you, Eliot! Didn’t know about the “If (not) last” trick!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Listing elements with/out commas and “and” before the last element’ is closed to new replies.