以不同格式显示第一项的 Wordpress 循环?

Ste*_*eve 0 wordpress wordpress-theming

我有一系列自定义帖子类型的帖子。他们每个人都有一个特色图片(缩略图)和一个摘录。

我想在主页上显示 4 个项目,第一个项目的格式与其他 3 个项目的格式不同,例如在附加图像中。这是怎么做的?

在此处输入图片说明

loQ*_*loQ 6

您可以为此使用计数器。请参阅下面的示例

 <?php
  $inc = 1;
  $the_query = new WP_Query();
  $the_query->query("posts_per_page=4");
  if ($the_query->have_posts()) : 
  while($the_query->have_posts()) : $the_query->the_post();
  if($inc == 1){ 
   //first post here
   //do stuffs here
  }
 else{
  //the rest of the posts
 }

 $inc++; //counter
 endwhile; 
 endif; 
 wp_reset_postdata();
 ?>
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用`$the_query-&gt;current_post` 来查看帖子索引。这样,无需创建计数器。 (2认同)