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?

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.