0

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();
  ?>

3 Answers 3

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();
?>
5
  • Thank you for your answer I have php errors that appear: It tells me that the () in else are too many. I remove them a new error appears: they are the {} of the while which appear in error syntax. I remove them I have the wp_reset_data in error syntax ...
    – Outkax
    Jul 14, 2021 at 14:37
  • If this is the error you're receiving (` Fatal error: Uncaught Error: syntax error, unexpected end of file in \wp-content\themes\picostrap5\front-page.php`) it just means that you probably forgot to close an if statement or something like that.
    – Pixelsmith
    Jul 14, 2021 at 16:57
  • With an endif and the () on the else I only have the last error : <?php $query = [ '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('mining-inc/loops/cards'); endif; } wp_reset_postdata(); ?>
    – Outkax
    Jul 14, 2021 at 18:46
  • I don't see the last error. Can you put it between ticks so it looks like this?
    – Pixelsmith
    Jul 14, 2021 at 20:36
  • Fatal error: Uncaught Error: syntax error, unexpected end of file in \themes\picostrap5\front-page.php on line 89 Pile d’appel : require_once() wp-blog-header.php:19 require() index.php:17[](null)
    – Outkax
    Jul 14, 2021 at 21:09
0

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();
  ?>
0

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();
  ?>
7
  • Thanks for your answer. Same problem as with the code above I have php errors that appear: It tells me that the () of the else are too much. I remove them a new error appears: they are the {} of the while that appear in error syntax. Fatal error: Uncaught Error: syntax error, unexpected end of file in \wp-content\themes\picostrap5\front-page.php on line 70 Pile d’appel : require_once() wp-blog-header.php:19 require() index.php:17
    – Outkax
    Jul 14, 2021 at 15:01
  • This code by itself does not have any errors. The error is most likely located somewhere else in your front-page.php file which is impossible to tell without seeing the actual code since all themes are different. Jul 14, 2021 at 17:17
  • I remove the code I have no more errors and vs code does not detect any either. I have tried with classic wordpress themes and I have the same errors
    – Outkax
    Jul 14, 2021 at 18:26
  • I dig the current_post correction!
    – Pixelsmith
    Jul 14, 2021 at 18:40
  • With an endif and the () on the else I only have the last error :/
    – Outkax
    Jul 14, 2021 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.