1

I'm using a shortcode for accordion. I would like to put a loop inside this shortcode. However the closing tag seems to be unparsed.

<?php echo do_shortcode('[su_accordion]');?>

<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        echo '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
        echo do_shortcode('[su_spoiler title="'.get_the_title().'" open="no" style="default" icon="plus" anchor="" class=""]'.get_the_content().'[/su_spoiler]');
        echo '</div>';

    endwhile;
else :
    echo wpautop( 'Sorry, no posts were found' );
endif;
?>

<?php echo do_shortcode('[/su_accordion]');?>

The last part of the shortcode is displayed as simple text.

Any solution?

3
  • collect all of the loop's output in a variable, wrap it in the accordion shortcode, then pass that through do_shortcode. Commented Nov 23, 2017 at 17:20
  • Hi Milo, I tried but how can I use loop inside a variable? I'll update the question with other code. Commented Nov 23, 2017 at 17:32
  • Hi Milo, I tried to put the loop inside a variable but it prints the loop like a string... Commented Nov 23, 2017 at 17:54

1 Answer 1

3

Collect all of the loop's output in a variable, wrap it in the accordion shortcode, then pass that through do_shortcode:

$output = '';   

if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        $output .= '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">';
        $output .= do_shortcode('[su_spoiler title="'.get_the_title().'" open="no" style="default" icon="plus" anchor="" class=""]'.get_the_content().'[/su_spoiler]');
        $output .= '</div>';

    endwhile;
else :
    $output = wpautop( 'Sorry, no posts were found' );
endif;

echo do_shortcode( '[su_accordion]' . $output . '[/su_accordion]' );

I've tested this with a couple of enclosing Shortcodes and got the expected output.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.