Home › Forums › General Issues › Repeater field and creating shortcode. › Reply To: Repeater field and creating shortcode.
Hey ckka,
So, the first thing you’ll need to do is determine what product type you’re looking at, so in your function, you could do something like this:
$product_type = get_field('product_type'); // Or whatever you've named your select field
Then, if you’ve got a ‘Combo’ product type, you’ll need to loop through each of the chosen post objects:
if ( $product_type == 'Combo' )
{
// get the Posts and deal with them here
$combination = get_field( 'combination' ); // or whatever you've named your repeater
if ( ! empty( $combination ) )
{
foreach ( $combination as $combination_item )
{
// each item is returned as an array with a key of 'post':
$post_id = $combination_item['post'];
}
}
}
else
{
// deal with the single item here
}
So, to put it all together, it might look something like this:
function prepare_stack( $field )
{
$page = get_the_ID();
$hidden = '';
$value = '
<div class="stack">';
$product_type = get_field( 'product_type' );
if ( $product_type == 'Combo' )
{
$combination = get_field( 'combination' );
if ( ! empty( $combination ) )
{
foreach( $combination as $combination_item )
{
$post_id = $combination_item['post'];
$value .= '
[acf field=\'pd_snip\' post_id=\''. $post_id . '\']';
$hidden .= '
[acf field=\'sc_text_link\' post_id=\''. $post_id . '\']';
}
}
}
else
{
$value .= '
[acf field=\'pd_snip\' post_id=\''. $page . '\']';
$hidden .= '
[acf field=\'sc_text_link\' post_id=\''. $page . '\']';
}
$value .= '
<b>[acf field=\'pd_price\' post_id=\''. $page . '\']</b>
[acf field=\'sc_add_to_cart_button\' post_id=\''. $page . '\']';
$value .= '
<span class="hide">'.$hidden.'
</span>';
$value .= '
</div>
';
$field['value'] = $value;
return $field;
}
add_filter('acf/prepare_field/key=field_5a64f5e14375a', 'prepare_stack');
You can see I used a variable called $hidden while I was looping through the combination posts so I could add it in later.. this means I don’t have to loop through twice.
This code is untested, so you should go through it line by line to make sure it does what you need it to do!
Hope this helps..
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.