admin管理员组

文章数量:1292141

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 question

I'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 question

I'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 0
Add a comment  | 

1 Answer 1

Reset to default 1

Unfortunatelly, 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