I'm trying to use a jquery toggle shortcode in a theme template.

The shortcode goes like this:

[sws_toggle3 title="Title"]You can now see the displayed text.[/sws_toggle3]

I want to pull posts from a specific category and use the toggle to display them in the page.

This is what my theme template currently has:

        $wp_query->query('cat=11&paged='  . $paged);
        while ($wp_query->have_posts()) : $wp_query->the_post();?>
        <li id="post-<?php the_ID(); ?>">
            <h2><a class="title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <div class="post">
            <?php the_content(); ?>
            <div class="clear"></div>
    <?php endwhile; ?>

With the output being something like:

[sws_toggle3 title="<?php the_title(); ?>"]<?php the_content(); ?>[/sws_toggle3]

Is this possible? Any help would be much appreciated.



You should be able to use do_shortcode(), with a few modifications:

echo do_shortcode( '[sws_toggle3 title="' . get_the_title() . '"]' . get_the_content() . '[/sws_toggle3]' );


If you need to maintain the formatting of the_content(), simply pass get_the_content() through the appropriate filter:

$content = get_the_content();
$content = apply_filters( 'the_content', $content );
echo do_shortcode( '[sws_toggle3 title="' . get_the_title() . '"]' . $content . '[/sws_toggle3]' );
  • ah, beat me to it! – Milo Jul 18 '11 at 2:09
  • More like, we both posted at exactly the same time! – Chip Bennett Jul 18 '11 at 2:23
  • This worked, but lost the formatting as well. – shinyidol Jul 18 '11 at 2:48
  • See update for how to maintain formatting. – Chip Bennett Jul 18 '11 at 11:04
  • I tried the update and still doesn't hold the formatting. Tried modifying, but still no dice. <?php $wp_query->query('cat=11&paged=' . $paged); while ($wp_query->have_posts()) : $wp_query->the_post(); $content = get_the_content(); $content = apply_filters( 'the_content', $content ); echo do_shortcode( '[sws_toggle3 title="' . get_the_title() . '"]' . $content . '[/sws_toggle3]' );?> <?php endwhile; ?> this is what i have right now – shinyidol Jul 24 '11 at 3:06

use do_shortcode():

$wp_query->query('cat=11&paged='  . $paged);

while ($wp_query->have_posts()) :

    $content = apply_filters( 'the_content',get_the_content() );
    $title = the_title('','',false);
    $shortcode = '[sws_toggle3 title="'.$title.'"]'.$content.'[/sws_toggle3]';
    echo do_shortcode($shortcode);

  • Thanks! I both of them worked to get the toggle to pull the content, the title and content lost all its formatting. – shinyidol Jul 18 '11 at 2:47

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.