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);
$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.
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!
<?php
$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='";
the_sub_field('photo');
echo "' width='300' height='300' alt='Photo of ";
the_sub_field('name');
echo "'><h3>";
the_sub_field('name');
echo "</h3><h2>";
the_sub_field('title');
echo "</h2><p>";
the_sub_field('phone');
echo "</p></div><div class='bio'>";
the_sub_field('biography');
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 ";
the_sub_field('name');
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>";
endwhile;
else :
// no rows found
endif;
?>
</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.
The topic ‘Shortcode in the loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.