Wordpress,输出文章的第一部分,然后输出文章的第二部分


Wordpress, out first part of a post, then output second part of a post

我目前使用高级自定义字段关系字段出一个帖子没有问题。然而,我需要一次浏览两篇文章。我的意思是,加载前两篇文章的第一部分,然后加载前两篇文章的第二部分,以此类推。帖子3、4的前两部分,帖子3、4的第二部分

<?php 
            $i = 0; 
            $posts = get_field('projects');
            if( $posts ): ?>
                <?php foreach( $posts as $post): 
                      $i++ ?>
                    <?php setup_postdata($post); ?>
                    <div class="col-sm-6">
                        <h3>
                            <?php the_title(); ?>
                        </h3>
                    </div>
                     <section>
                            <div class="container">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <?php the_post_thumbnail(); ?>
                                    </div>
                                    <div class="col-sm-6">
                                        <?php the_title(); ?>
                                    </div>
                                </div>
                            </div>
                        </section>
                    <?php if( $i == 2): $i = 0; ?>
                        <div class="clear"></div>
                     <?php endif; ?>
                <?php endforeach; ?>
                <?php wp_reset_postdata();  ?>
            <?php endif; ?>

输出应该是这样的:

<div></div>//part 1 first post
<div></div>//part 1 2nd post
<section></section>//part 2 first post
<section></section>//part 2 2nd post
<div></div>//part 1 third post
<div></div>//part 1 fourth post
<section></section>//part 2 third post
<section></section>//part 2 fourth post

我知道这不是你想要的,但它应该让你接近球场?

<?php 
     $i = 0; 
     $posts = get_field('projects');
     $temp_string_top = "";
     $temp_string_bottom = "";
     $output_string = "";
     if( $posts ): 
         foreach( $posts as $post): 
             $i++;
             setup_postdata($post);
             $temp_string_top += '<div class="col-sm-6"><h3>'.the_title().'</h3></div>';
             $temp_string_bottom += '<section><div class="container"><div class="row"><div class="col-sm-6">';
             $temp_string_bottom += the_post_thumbnail().'</div><div class="col-sm-6">'.the_title().'</div></div></div></section>';
              if( $i == 2){
                    $i = 0;
                    $output_string += $temp_string_top;
                    $output_string += $temp_string_bottom;
                    $output_string += '<div class="clear"></div>';
                    $temp_string_top = "";
                    $temp_string_bottom = "";
              }
          endforeach;
          wp_reset_postdata(); 
       endif;
       echo $output_string;   // oops.. almost forgot this!
?>

我没有测试这个,但我很确定它会为你工作。