Support

Account

Home Forums ACF PRO Shortcode in the loop Reply To: Shortcode in the loop

  • @trishah I think you will need to better explain how you want to add the counter variable to a shortcode. Can you share your loop code, or perhaps even an example of how you want the shortcode to look after the counter variable has been added? Is the variable being added as a parameter? I’m trying to figure out what your goal is and why you would want to add a counter variable to a shortcode in the first place.

    The only reason I could see you wanting to add the counter variable value to a shortcode is if you were appending the value to a class property or something. If that is the case you could do something like.

    $counter = 1;
    if( have_rows('repeater_field') ) : while( have_rows('repeater_field') ) : the_row();
        $contactUs = get_sub_field('contact_form_shorcode');
        $newClass = 'class="instance-' . $counter . '"';
        $short = str_replace( 'class=""', $newClass, $contactUs);
        echo do_shortcode($short);
        $counter++;
    endwhile; endif;

    If you can provide more info, especially what you want the output to be, I may be able to provide more specific help.