Bricks Builder: Custom Loop Index for Dynamic Attributes

This code snippet introduces a custom function for Bricks Builder that provides a user-friendly loop index starting from 1.

The function, get_loop_index(), retrieves and increments Bricks’ native loop index. It’s registered with Bricks’ echo functions, allowing direct use in the builder’s code elements.

This approach enables targeted styling or JavaScript interactions based on an item’s position in the loop, enhancing design flexibility and interactivity in Bricks Builder projects.

function get_loop_index() {
    $index = intval( Bricks\Query::get_loop_index() ) + 1; //start from 1
    if ($index) return $index;
}

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'get_loop_index'
  ];
} );