wp query – post loop with different design depending on post
Question
I’m having a lot of trouble finding the solution.
I would like to loop my last 3 posts with wp-query.
I would like the 1st post to be in one div and the last 2 posts in another different div.
So when I publish a new post the last post goes to div 1 and the previous one goes down to the 2nd div etc…
here is an example
$args = array(
'post_type' => 'post',
'post_per_page' => 3,
);
<?php $loop = new WP_Query($args);
$count = 0;
while ($loop->have_posts()) : $loop->the_post();
if ($count == 0):
?>
<!-- last post -->
<div class="post-one">
<h1><?php the_title();?></h1>
</div>
<?php else:?>
<!-- second et third post -->
<div class="post-two">
<h2><?php the_title();?></h2>
<?php the_excerpt();?>
</div>
<?php
$count ++;
endif;
endwhile;
wp_reset_postdata();?>
0
2 months
2023-09-25T10:50:02-05:00
2023-09-25T10:50:02-05:00 0 Answers
0 views
0
Leave an answer