Support » Plugin: Custom Content Shortcode » ‘for-each’ with ‘exists’

  • I am trying to build a shortcode to display several categories of academic degrees within a for-each loop of academic departments.

    The basic structure is:
    [for each=department]
    [each name]
    CONDITIONAL HEADER
    [loop type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=ug compare_2=and list=true][field title-link][/loop]
    CONDITIONAL HEADER
    [loop type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=grad compare_2=and list=true][field title-link][/loop]
    [/for]

    In some departments, one of the loops will be empty. Because of that, the header needs to display only if there are posts in the loop. I found two ways (below) to make the header display only when there are posts in the loop, but both of them break any ‘for-each’ statements below them on the page.

    – This works to make the header conditional but breaks any following for-each:
    [if exists type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=ug compare_2=and]<h6>Undergraduate Majors</h6>[/if]
    – This works to make the header conditional but also breaks any following for-each:
    [loop exists type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=ug compare_2=and list=true]<h6>Undergraduate Majors</h6>
    [the-loop][field title-link][/the-loop]
    [/loop]
    – This DOES NOT break anything following and does not display the header if the loop is empty, but (as it should) it loops through and writes the header once for each post, so if there are 4 posts in the list it writes the header 4 times:
    [loop type=academics taxonomy=division term={TERM}][if taxonomy=degree term=ug]<h6>Undergraduate Majors</h6>[/if][/loop]

    It seems like the conflict comes from ‘exists’, and ‘exists’ doesn’t break the for-each it’s in but breaks any for-each that follows. Is this a bug, or is my syntax wrong?
    Thx, Susan

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think you can do it with [if first] CONDITIONAL HEADER [/if] at the beginning of the loop, so it will only show if there is a first item.

    Peter

    Thanks for your help.

    – [loop type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=ug compare_2=and list=true] [if first] CONDITIONAL HEADER [/if][field title-link][/loop]
    displays the conditional headers where they should be, but it makes the first item in the list

  • CONDITIONAL HEADER
    item 1
  • so the formatting is off.

    – this never displayed the header more than once, but it skipped some! I’m not sure why, because without count=1 it always displays the header once for each item in the loop:
    [loop type=academics taxonomy=division term={TERM} count=1][if taxonomy=degree term=ug]<h6>Undergraduate Majors</h6>[/if][/loop]

    – What finally worked is:
    [loop type=academics taxonomy=division term={TERM} taxonomy_2=degree term_2=ug compare_2=and count=1]CONDITIONAL HEADER[/loop]

    It seems that there might be a bug with ‘exists’. Maybe it is not resetting properly?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘‘for-each’ with ‘exists’’ is closed to new replies.