您可以使用current_post
字段WP_Query
检查循环内部,也可以将循环分为两个循环。以下是代码示例:
<?php
$services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
while ( $services_loop->have_posts() ) : $services_loop->the_post();
?>
<section class="single-service">
<div class="container">
<div class="row">
<div class="col-md-6">
<?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
</div>
<div class="col-md-6">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?></div>
</div>
</div>
</div>
</section>
<?php if ( 1 == $services_looop->current_post ) : ?>
<div>
Custom Content
</div>
<?php endif; ?>
<?php
endwhile;
wp_reset_postdata();
?>
或者将循环拆分为两个循环的解决方案:
<?php
$services_loop = new WP_Query( array( \'post_type\' => \'service\', \'posts_per_page\' => -1 ) );
while ( $services_loop->have_posts() && $services_loop->current_post < 2 ) :
$services_loop->the_post();
?>
<section class="single-service">
<div class="container">
<div class="row">
<div class="col-md-6">
<?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
</div>
<div class="col-md-6">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?></div>
</div>
</div>
</div>
</section>
<?php endwhile; ?>
<div>
Custom Content
</div>
<?php while ( $services_loop->have_posts() ) : $services_loop->the_post(); ?>
<section class="single-service">
<div class="container">
<div class="row">
<div class="col-md-6">
<?php the_post_thumbnail(\'full\', array(\'class\' => \'img-fluid w-100\', \'alt\' => get_the_title(), \'title\' => get_the_title())); ?>
</div>
<div class="col-md-6">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?></div>
</div>
</div>
</div>
</section>
<?php
endwhile;
wp_reset_postdata();
?>