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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用