Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

    <?php
        $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>
            <div class="clear"></div>
        </li>
    <?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.

Thanks

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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

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

EDIT

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

<?php
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
echo do_shortcode( '[sws_toggle3 title="' . get_the_title() . '"]' . $content . '[/sws_toggle3]' );
?>
share|improve this answer
    
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
add comment

use do_shortcode():

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

while ($wp_query->have_posts()) :

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

endwhile;
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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