Home Forums ACF PRO Shortcode in the loop


Shortcode in the loop

  • Adding a shortcode to a template is easy using:

    $contactUs = get_field( "contact_form_shortcode" );
     $short = $contactUs;
     echo do_shortcode($short);

    Now I need to add a shortcode to a repeater loop. I’m already using a counter++ to identify the different instances but I’m at a loss on how to add a counter value to the above code (if that is even the correct way to do this).

    Your help is greatly appreciated!

  • @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);
    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.

  • Thank you @kiwicreative for the feedback. Here is more information so you may better see the issue. The below code works except for the contact form shortcode.

    First, this is for a Board of Directors page where each board member has their own contact form, i.e., shortcode.

    Second, the counter is used for a Bootstrap4 collapse area to hide each members contact form until it is opened upon being clicked.

    The below code is working… all but the contact forms (see near bottom of code). I suspect this is because there is no differentiation between each form because the code is using $short over and over again.

    Please let me know if you need more information. And thank you!

    $count = 1;
    if( have_rows('directors_and_officers') ):
        while ( have_rows('directors_and_officers') ) : the_row();
        $member = $count++;
        echo "<div class='officer'><div class='photo'><img src='";
        echo "' width='300' height='300' alt='Photo of ";
        echo "'><h3>";
        echo "</h3><h2>";
        echo "</h2><p>";
        echo "</p></div><div class='bio'>";
        echo "<button class='btn btn-primary' type='button' data-toggle='collapse' data-target='#collapse-";
        echo $member;
        echo "' aria-expanded='false' aria-controls='collapse-";
        echo $member;
        echo "'>Contact ";
        echo "</button>
            <div class='collapse' id='collapse-";
        echo $member;
        echo "'>
            <div class='card card-body'>";
            $contactUs = get_field( "contact_form_shortcode" );
            $short = $contactUs;
        echo do_shortcode($short);
        echo "</div></div></div></div>";
    else :
        // no rows found
    </div><!-- #officers -->
  • I found the answer! Instead of using:

    $contactUs = get_field( "contact_form_shortcode" );
    $short = $contactUs;
    echo do_shortcode($short);

    All I needed to add was this:

    do_shortcode( get_sub_field('contact_form_shortcode') );

    Thank you for trying to help.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Shortcode in the loop’ is closed to new replies.