Reputation: 445
I need help. I don’t understand what is happening. I created a shortcode. There he is
function mainslider_function() {
function lesson_slider() {
$open_div = '<div class="autoplay">';
$close_div = '</div>';
if( have_rows('test_fields', 'option') ):
echo $open_div;
while ( have_rows('test_fields', 'option') ) : the_row();
$sub_value = get_sub_field('image');
echo '<div class="slide"><img src="'.$sub_value.'"></div>';
endwhile;
echo $close_div;
endif;
}
return lesson_slider();
}
And it works. But only to the front. When trying to edit a page with this shortcode. The WordPress editor stops working. I understand that the problem is that I use a function in a function. Because when I test this code:
function mainslider_function() {
$test = 'test message';
return $test;
}
Everything is working fine. And if I do like this
function mainslider_function() {
function test(){
$test = 'test message';
echo test;
}
return test();
}
The editor stops working. Tell me, please why this is happening?
Upvotes: 1
Views: 105
Reputation: 14927
You don't need an inner function. You could concatenate your string parts or use output buffering.
With the latter, this becomes:
function mainslider_function()
{
ob_start();
if (have_rows('test_fields', 'option')) {
echo '<div class="autoplay">';
while (have_rows('test_fields', 'option')) {
the_row();
$sub_value = get_sub_field('image');
echo '<div class="slide"><img src="', $sub_value, '"></div>';
}
echo '</div>';
}
return ob_get_clean();
}
Note: also did a bit of code cleanup in the process.
Upvotes: 1