admin管理员组文章数量:1134247
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();?>
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();?>
Share
Improve this question
edited Sep 25, 2023 at 17:02
asked Sep 25, 2023 at 15:50
user188850user188850
2 Answers
Reset to default 2Your $count++
needs to be after your endif
statement. It's not counting up because your counter is always at 0 which means you're always in your "if" portion of your statement not your "else".
Like this:
$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
endif;
$count ++;
endwhile;
wp_reset_postdata();?>
You have error in code
if ($count == ):
it should be
if ($count == 0):
本文标签: wp querypost loop with different design depending on post
版权声明:本文标题:wp query - post loop with different design depending on post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736793023a1953170.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论