Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8842517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:58:43+00:00 2026-06-14T10:58:43+00:00

im using shortcode in a php template file for wordpress, because the shortcode opens

  • 0

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]');

         ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:58:44+00:00Added an answer on June 14, 2026 at 10:58 am

    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]');
     ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inserted a set of tabs to my php wordpress template using the
I'm a bit stuck on this - I am using a Wordpress shortcode with
I am trying to create a Wordpress shortcode-style feature in PHP to replace shortcodes
I have activated TDO Mini Forms plugin and using the shortcode in my template
Using the Redis info command, I am able to get all the stats of
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
I am using jcart http://conceptlogic.com/jcart/ to create some cart functionality in wordpress. I am
I have wordpress page with content and image gallery. when i print_r my content,it
I'm using car66 plugin in my wordpress site and I want to use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.