Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Shortcodes in the Repeater Field For Advanced Custom Fields WordPress

  • SOLVED

I am completely stuck. Several hours deep and still where I started. Cannot for the life of me get the repeater field to render shortcodes and am going a little nuts

Tried this : http://support.advancedcustomfields.com/discussion/889/solved-execute-shortcode-in-repeater-field/p1 but not sure I completely understand the syntax of the answer, it is pretty far from the normal repeater field

What I am essentially doing is making a list of woocommerce products, and all the user does is type in the ids:

The shortcode: [product_page id="modal_div"]
Repeater Field: 'modals'
Sub Field: 'modal_div'

Standard Markup for the Repeater Field:

if(get_field('')):
while(has_repeater_field('')):
the_sub_field('');
the_sub_field('');
endif;
endwhile;


This is what I have been trying, in many variations without luck
<?php
if(get_field('modals')):
while(has_sub_field('modals')):
$productid = the_sub_field('modal_div');
echo do_shortcode('[product_page id="'.$productid.'"]');
endwhile;
endif;
?>

Answers (2)

2012-08-11

Dbranes answers:

I haven't used this "Advanced Custom Fields" but here are some general thoughts:

- is the shortcode working outside the while-loop?
- where are you running the code?
- is the_repeater_field('modals') non-empty?
- is $productid = the_sub_field('modal_div') non-empty?
- is echo do_shortcode('[product_page id="123"]') working for some given id?

ps: the_repeater_field() has been [[LINK href="http://www.advancedcustomfields.com/docs/functions/the_repeater_field/"]]deprecated[[/LINK]] since v3.3.4 and replaced with
[[LINK href="http://www.advancedcustomfields.com/docs/functions/has_sub_field/"]]has_sub_field()[[/LINK]]


Kyle comments:

Yes the shortcode works outside of the if statement
I am running the code in a page template
The fields are not empty and returning properly when not inside the shortcode

Thank you for picking up on the deprecated function! I never would have caught that


Dbranes comments:

ok, so you are seeing output if you place an echo in front of:

echo $productid = the_sub_field('modal_div');


Kyle comments:

Well, not the way I expected the question to be answered haha

That worked!!

I guess the new has_sub_field() function is better with shortcodes, I appreciate the help


Dbranes comments:

ok great ;-)


Kyle comments:

I'll just post my complete code for anyone who comes across this in the future.

I also changed out the_sub_field for get_sub_field (which I found in the link you posted)

<?php
if(get_field('modals')):
while(has_sub_field('modals')):
$product = get_sub_field('modal_div');
echo do_shortcode('[product_page id="'.$product.'"]');
endwhile;
endif;
?>

2012-08-11

Basilis Kanonidis answers:

Check this out, is it working? :)


<?php

if(get_field('modals')):

while(the_repeater_field('modal_div')):

echo do_shortcode(.'['.the_sub_field('modal_div').']'.);

endwhile;

endif;

?>


Kyle comments:

That actually broke the template

Thanks for the reply though


Basilis Kanonidis comments:

<?php
if( get_field('modal') ): ?>
<?php while( has_sub_field('modal') ): ?>


<?php if( get_sub_field('modal_div) ): ?>

<?php while( has_sub_field('modal_div') ): ?>

<?php echo do_shortcode(.'[.'acf ="field_name"'.]'.); ?>

<?php endwhile; ?>

<?php endif; ?>


<?php endwhile; ?>
<?php endif; ?>



That should work!