Ask your WordPress questions! Pay money and get answers fast! (more info)

do_shortcode with ACF Repeater Fields WordPress

  • SOLVED

I need major syntax help.

I am using Ultimate Shortcodes for Wordpress and want to use the spoiler/accordion shortcode in a page template that is propagated by the advanced custom fields and the repeater add-on.

HTML version of shortcode looks like this.

<div id="a4e_spoiler">
[a4e_spoiler title="This is my accordion title" class="a4e-custom-spoiler"]
This is my hidden content that shows when a user click on the title.
[/a4e_spoiler]</div>


I want it to look something like this and actually work ;)

My variables are
a4e-spoilers (field type repeater)
a4e-spoiler-title (sub_field)
a4e-spoiler-content (sub_field)

My sloppy broken template code that needs fixing. ;)


// Adding our custom content output

add_action( 'genesis_entry_content', 'genesis_a4e-spoilers', 10, 2 );
add_action( 'genesis_post_content', 'genesis_a4e-spoilers', 10, 2 );


// The Custom Content output function

function genesis_a4e-spoilers() { ?>
<!-- Spoiler Accordions Layout ACF -->

<?php if (get_field('a4e-spoilers')): ?>

<div class="a4e-spoilers-group">

<?php while(has_sub_field("a4e-spoilers")): ?>

<?php echo do_shortcode('[a4e_spoiler title="get the_sub_field('a4e-spoiler-title')" class="a4e-custom-spoiler"]<?php the_sub_field('a4e-spoiler-content'); ?>
[/a4e_spoiler]</div>

<?php endwhile; ?>

</div>

<?php endif; ?>

<?php

}
genesis();

Answers (1)

2014-04-13

Duncan O'Neill answers:

Hi there,

what, if any, error message is showing on the page when you load it? What error messages are showing in your logs?

best,

Duncan


Jeffrey comments:

Hi Duncan,

No error messages in log or displayed.

I know for a fact my shortcode syntax and maybe getfield syntax is flawed and I just need someone to fix it for me. ;)
Either using <?php or the echo option. I honestly don't know but whichever way is correct would be good. :0)

For those that know it would likely take them 10 minutes or less. I however don't know how, which is why I defer to you the experts. ;)

Thanks,
Jeffrey


Duncan O'Neill comments:

Try replacing this;

<?php echo do_shortcode('[a4e_spoiler title="get the_sub_field('a4e-spoiler-title')" class="a4e-custom-spoiler"]<?php the_sub_field('a4e-spoiler-content'); ?>

[/a4e_spoiler]</div>


with this;


<?php

echo(do_shortcode('[a4e_spoiler title=". get the_sub_field('a4e-spoiler-title') . " class="a4e-custom-spoiler"]' . the_sub_field('a4e-spoiler-content') . '[/a4e_spoiler]'));

?>
</div>



Jeffrey comments:

Duncan,

It still outputs a completely blank screen. If I view source it too is blank.

Maybe there is an error somewhere else in the template?


Duncan O'Neill comments:

Hi Jefrrey,

easy way to test whether it's that bit of code I posted; replace it all with something basic, like;


<?php echo "test"; ?>
</div>


If the page is no longer blank, the error's in the piece of code I gave you, and your original code. If it's still blank, the error is elsewhere.

best,


Duncan O'Neill comments:

Gonna test code properly with editor on local site. Back in a bit.

best,

D


Jeffrey comments:

Hi,

I got the Echo Test part working.

This is the working template I had dashes instead an underscore in both the add_action and function line.

However this still means the original code (below) you supplied is not valid in some way, as the blank page persists with that code in place.

<?php

echo(do_shortcode('[a4e_spoiler title=". get the_sub_field('a4e-spoiler-title') . " class="a4e-custom-spoiler"]' . the_sub_field('a4e-spoiler-content') . '[/a4e_spoiler]'));

?>


And if it helps, here is the template file I got working with the Echo Test

<?php

// Adding our custom content output
add_action( 'genesis_entry_content', 'genesis_a4e_spoilers', 10, 2 );
add_action( 'genesis_post_content', 'genesis_a4e_spoilers', 10, 2 );


// The Custom Content output function

function genesis_a4e_spoilers() { ?>


<?php if (get_field('a4e-spoilers')): ?>

<div class="a4e-spoilers">

<?php while(has_sub_field("a4e-spoilers")): ?>



<div id="a4e-accordion" class="single-accordion">
<?php echo "test"; ?>
</div>


</div>
<?php endwhile; ?>

</div>

<?php endif; ?>



<?php

}

genesis();


Duncan O'Neill comments:

Thanks Jeffrey, that helps. I'll get back to asap.


Duncan O'Neill comments:

Hi Jeffrey,

sorry, got unavopidably interrupted.

I can only really test for syntax, but this works;

in between the &lt;div&gt; which is


<div id="a4e-accordion" class="single-accordion">
<code>

and its following closing &lt;div&gt; , use this code;

<code>
<?php

$spTitle = the_sub_field('a4e-spoiler-title');

echo(do_shortcode('[a4e_spoiler title="$spTitle" class="a4e-custom-spoiler"]' . the_sub_field('a4e-spoiler-content') . '[/a4e_spoiler]'));

?>



Jeffrey comments:

Almost.

It shows the accordion but the title and content are rendered above and outside of it.

See bottom two on the screen capture. The other ones at the top of the page are regular hand coded html (my working samples).