Adrian
Adrian

Reputation: 2012

how to echo contents of wordpress loop as one variable outside loop

im using shortcode in a php template file for wordpress, because the shortcode opens and closes I need to get the entire content within it as one variable. In this case the content is a wordpress loop.

So far what I have only displays the final post of the loop. I understand why, as that is what the final value of the variable is. Im wondering could someone help me get the entire contents (ie all three posts) into a variable, as oppose to just the final post.

Thanks

<?php 
                $news_title .= '';
                $news_single_post .= '';

            if ( have_posts() ) :
            $the_query = new WP_Query( array ( 'posts_per_page' => 3, 'cat' => 1 ) ); /*  */
             while ($the_query->have_posts() ) : $the_query->the_post(); ?>
            <?php 
                $news_title = get_the_title();
                $news_excerpt = get_the_excerpt();
                $news_single_post = '<div class="home-content-news-title">'.$news_title.'</div><div class="home-content-news-excerpt">'.$news_excerpt.'</div>';

                endwhile;
                wp_reset_postdata();

                endif; 



                $news_tab_title_string = 'News';
                $news_tab_title_shortcode = do_shortcode('[wptabtitle]'.$news_tab_title_string.'[/wptabtitle]');
                $news_tab_content_shortcode = do_shortcode('[wptabcontent]'.$news_single_post.'[/wptabcontent]');
                $news_tab = $news_tab_title_shortcode.$news_tab_content_shortcode;
                echo do_shortcode('[wptabs]'.$news_tab.'[/wptabs]');

         ?>

Upvotes: 1

Views: 2291

Answers (1)

Maccath
Maccath

Reputation: 3956

Define the variable you want all of the content to go into above and outside the loop e.g. $variable = '';, and then within the loop concatenate to that variable using $variable .= $content_to_concat; and then use echo $variable; outside of the loop at the end to print the entire contents.

Your code as an example:

<?php 
    $news_title .= '';
    $news_single_post .= '';
    $news_all_posts = ''; // Define the variable

    if ( have_posts() ) :
    $the_query = new WP_Query( array ( 'posts_per_page' => 3, 'cat' => 1 ) ); /*  */

    while ($the_query->have_posts() ) : $the_query->the_post();

    $news_title = get_the_title();
    $news_excerpt = get_the_excerpt();
    $news_single_post = '<div class="home-content-news-title">'.$news_title.'</div><div class="home-content-news-excerpt">'.$news_excerpt.'</div>';

    $news_all_posts .= $news_single_post; // Add each post to the variable

    endwhile;
    wp_reset_postdata();

    endif;

    $news_tab_title_string = 'News';
    $news_tab_title_shortcode = do_shortcode('[wptabtitle]'.$news_tab_title_string.'[/wptabtitle]');

    // Use the variable to display the content
    $news_tab_content_shortcode = do_shortcode('[wptabcontent]'.$news_all_posts.'[/wptabcontent]');

    $news_tab = $news_tab_title_shortcode.$news_tab_content_shortcode;
    echo do_shortcode('[wptabs]'.$news_tab.'[/wptabs]');
 ?>

Upvotes: 4

Related Questions