duanheye7423 2012-11-16 14:32
浏览 53
已采纳

如何将wordpress循环的内容作为一个外部循环变量回显

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条回答 默认 最新

  • duan7007 2012-11-16 14:34
    关注

    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]');
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器