admin管理员组

文章数量:1289565

I am creating a theme for my site. I would like to display the last 5 articles on the home page but with a different display for the first articles. So I created this code that I try to tweak but impossible to display the first article with the first template and the 4 others with the second template. I don't know if it's possible to do it the way I want to do it, but this way seems to be the easiest

My code :

<?php
  $query = [
    'post_type' => 'post',
    'posts_per_page' => 5
  ];
  $query = new WP_Query($query);
  while ($query->have_posts()) : {
    $query->the_post();
    if($query === 1) :
      get_template_part('loops/acc-cards');
    else() :
      get_template_part('loops/cards');
  }
  wp_reset_postdata();
  ?>

I am creating a theme for my site. I would like to display the last 5 articles on the home page but with a different display for the first articles. So I created this code that I try to tweak but impossible to display the first article with the first template and the 4 others with the second template. I don't know if it's possible to do it the way I want to do it, but this way seems to be the easiest

My code :

<?php
  $query = [
    'post_type' => 'post',
    'posts_per_page' => 5
  ];
  $query = new WP_Query($query);
  while ($query->have_posts()) : {
    $query->the_post();
    if($query === 1) :
      get_template_part('loops/acc-cards');
    else() :
      get_template_part('loops/cards');
  }
  wp_reset_postdata();
  ?>
Share Improve this question edited Jul 15, 2021 at 13:32 Outkax asked Jul 13, 2021 at 17:46 OutkaxOutkax 14 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

I haven't tested this, but it looks like you just need to set an iteration variable (usually abbreviated $i), like this:

<?php
$i = 0; // This sets your variable
$query = [
'post_type' => 'post',
'posts_per_page' => 5
];

$query = new WP_Query($query);
  while ($query->have_posts()) : {
    $query->the_post();
    if($i == 0) :
      get_template_part('loops/acc-cards');
    else() :
      get_template_part('loops/cards');
  }
$i++ // This iterates the iteration variable $i
wp_reset_postdata();
?>

Solution found, it missed an endwhile;

 <?php
  $query = array(
    'post_type' => 'post',
    'posts_per_page' => 5
  );
  $query = new WP_Query($query);
  while ($query->have_posts()) : 
    $query->the_post();
    if($query->current_post === 0) :
      get_template_part('mining-inc/loops/acc-cards');
    else :
      get_template_part('loops/cards');
    endif;
  endwhile;
  wp_reset_postdata();
  ?>

The proper day to do it in WordPress is to use the current_post value instead of setting your own variable like $i. current_post is the index of the current post in a loop that starts with 0.

<?php
  $query = array(
    'post_type' => 'post',
    'posts_per_page' => 5
  );
  $query = new WP_Query($query);
  while ($query->have_posts()) :
    $query->the_post();
    if($query->current_post === 0) :
      get_template_part('loops/acc-cards');
    else:
      get_template_part('loops/cards');
    endif;
  endwhile;
  wp_reset_postdata();
  ?>

本文标签: phpDisplay articles with a different template in the home pageSolved