Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1292141
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm trying to divide a posts loop in order to show 4 posts per each slide inside a slider.
The code below works good, but since the number of posts in the Wordpress panel is dynamic and varies, it breaks and doesn't close the last item depending on the total number of posts.
Is there a way I can set some sort of counter so it works no matter if there is a total of 8,9, 11, 12... posts?
<!-- Slider -->
<div class="slider">
<?php $argsb = array('post_type'=>'post', 'posts_per_page' => -1, 'post_status' => 'publish');?>
<?php $blog = new WP_Query($argsb); if ( $blog->have_posts() ): ?>
<!-- Slide -->
<?php while ( $blog->have_posts() ) : $blog->the_post(); $i++;?>
<?php if( $blog->current_post % 4 == 0 ) echo "\n".'<div class="slide">'."\n"; ?>
<h1><?php echo the_title(); ?></h1>
<?php if( $blog->current_post % 4 == 3 ) echo '</div>'."\n"; ?>
<!-- end of slide -->
<?php endwhile; wp_reset_postdata();?>
<?php endif; ?>
</div>
Thanks!
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 3 years ago.
Improve this questionI'm trying to divide a posts loop in order to show 4 posts per each slide inside a slider.
The code below works good, but since the number of posts in the Wordpress panel is dynamic and varies, it breaks and doesn't close the last item depending on the total number of posts.
Is there a way I can set some sort of counter so it works no matter if there is a total of 8,9, 11, 12... posts?
<!-- Slider -->
<div class="slider">
<?php $argsb = array('post_type'=>'post', 'posts_per_page' => -1, 'post_status' => 'publish');?>
<?php $blog = new WP_Query($argsb); if ( $blog->have_posts() ): ?>
<!-- Slide -->
<?php while ( $blog->have_posts() ) : $blog->the_post(); $i++;?>
<?php if( $blog->current_post % 4 == 0 ) echo "\n".'<div class="slide">'."\n"; ?>
<h1><?php echo the_title(); ?></h1>
<?php if( $blog->current_post % 4 == 3 ) echo '</div>'."\n"; ?>
<!-- end of slide -->
<?php endwhile; wp_reset_postdata();?>
<?php endif; ?>
</div>
Thanks!
Share Improve this question edited May 20, 2021 at 5:56 Anta asked May 19, 2021 at 15:06 AntaAnta 291 silver badge7 bronze badges 01 Answer
Reset to default 1Unfortunatelly, I didn't understand your question right, but I see only 2 possible problems here. Here you will need to use:
$blog->current_post
- current post index
$blog->post_count
- total posts
If you need to wrap each 4 posts with a div, and if there are less then 4 posts in the end, wrap them to. To achieve this you need to add another OR condition to the second if statement. Check if the current post is equal to the last post.
//before...
if( $blog->current_post % 4 == 3 )
//after...
if( $blog->current_post % 4 == 3 || $blog->current_post == $blog->post_count - 1)
If you want to wrap each 4 posts in a div and "leave" the remainder, you can add another if statement, which should go first, just after while loop starts.
//your while loop starts
while ( $blog->have_posts() ) : $blog->the_post();
//break the loop if there are less than 4 posts left
if( $blog->current_post >= $blog->post_count - ($blog->post_count % 4)){
break;
}
本文标签: postsMake loop inside slider divisible
版权声明:本文标题:posts - Make loop inside slider divisible 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741546989a2384651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论