Title: Increment a variable in a loop
Last modified: July 12, 2020

---

# Increment a variable in a loop

 *  Resolved [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * (@studioavanti)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/increment-a-variable-in-a-loop/)
 * 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)

 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/increment-a-variable-in-a-loop/#post-13115928)
 * 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 5 years, 11 months ago by [polarracing](https://wordpress.org/support/users/polarracing/).
 *  Thread Starter [studioavanti](https://wordpress.org/support/users/studioavanti/)
 * (@studioavanti)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/increment-a-variable-in-a-loop/#post-13116917)
 * Hi [@polarracing](https://wordpress.org/support/users/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]
       ```
   
 *  [polarracing](https://wordpress.org/support/users/polarracing/)
 * (@polarracing)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/increment-a-variable-in-a-loop/#post-13117351)
 * 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.

 * ![](https://s.w.org/plugins/geopattern-icon/custom-content-shortcode_766976.svg)
 * [Custom Content Shortcode](https://wordpress.org/plugins/custom-content-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/custom-content-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/custom-content-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/custom-content-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/custom-content-shortcode/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [polarracing](https://wordpress.org/support/users/polarracing/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/increment-a-variable-in-a-loop/#post-13117351)
 * Status: resolved