Support » Plugin: Custom Content Shortcode » Increment a variable in a loop

  • Resolved studioavanti

    (@studioavanti)


    Hi,

    Do you think it’s possible to increment a variable set with CCS?
    I need this inside an ACF [related]:

    [related my_acf_relationship]
        [set id_count]0[/set]
        [here something magic to increment the var on each loop cycle]
        [pass vars]
            <a href="#{ID_COUNT}">My link {ID_COUNT}</a>
        [/pass]
    [/related]

    Given i have 3 related items, it would return this:

    <a href="#1">My link 1</a>
    <a href="#2">My link 2</a>
    <a href="#3">My link 3</a>

    I tried to use CCS [calc] without sucess.

    Thanks for your help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Use

    
    [set id_count]0[/set]
      [related my_acf_relationship]  
        [set id_count][calc][id_count] + 1[/calc][/set]
        [pass vars]
            <a href="#{ID_COUNT}">My link {ID_COUNT}</a>
        [/pass]
    [/related]

    You have to set the id_count outside the [related]. If not, you always reset the newly calculated id_count for the next related to 0.

    • This reply was modified 7 months, 2 weeks ago by polarracing.
    Thread Starter studioavanti

    (@studioavanti)

    Hi @polarracing,

    Thanks for your suggestion, well done!
    It works after properly fetching the variable: replacing [id_count] by [get id_count]:

    [set id_count]0[/set]
    [related my_acf_relationship]  
        [set id_count][calc][get id_count] + 1[/calc][/set]
        [pass vars]
            <a href="#{ID_COUNT}">My link {ID_COUNT}</a><br>
        [/pass]
    [/related]

    Yeah of course it must get id_count

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Increment a variable in a loop’ is closed to new replies.