我试图在不同的div中显示帖子的标题和帖子内容,使用循环在$the\\u查询中迭代帖子,如何实现这一点?
<?php $the_query = new WP_Query( \'posts_per_page=4\' );?>
结构应该是这样的
<div class="grid">
<div class="main post">
<!--show data of the first post in the array-->
</div>
<div class="nested">
<div class="first post">
<!--show data of the second post in the array-->
</div>
<div class="second post">
<!--show data of the third post in the array-->
</div>
<div class="third post">
<!--show data of the fourth post in the array-->
</div>
</div> <!--end nested-->
</div> <!--end grid-->
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
PHP循环是循环-您可以控制它们;)
<?php
$the_query = new WP_Query( array(\'posts_per_page\' => 4) );
if ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="grid">
<div class="main post">
<!--show data of the first post in the array-->
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<div class="nested">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post">
<!--show data of next post in the array-->
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div> <!--end nested-->
</div> <!--end grid-->
<?php endif; ?>