0

Here is the Code that works for 3 items, and if there is exact multiples of 3 items (ie, if we have 9 items) then it will add an extra blank div at the end.

if there is items that is not multiples of 3 (ie, if we have 8 items) then it works fine. I am not a programmer,

so any help will be appreciated:

Bellow are the code sample:

<div class="row">
    <?php 
       $i = 1;
       $wp_query = new WP_Query( array( 'posts_per_page' => -1, 
                  'post_type' => 'projects' ) );
      echo '<div class="panel">';
      if ( $wp_query->have_posts() ) : 
         while ( $wp_query->have_posts() ) : $wp_query->the_post();
         the_title();         
       if($i % 3 == 0) { echo '</div><div class="panel">'; }
     $i++; 
      endwhile; endif;
     echo '</div>';    
  ?>

</div>
3
  • Check for the value of $i. If the value of '$i%9' is 0. Then add an extra div at the end of while loop. Commented Jun 23, 2016 at 9:26
  • You mean add one extra div at the end? if we have 9 items? Commented Jun 23, 2016 at 9:28
  • You might look into the function array_chunk Commented Jun 23, 2016 at 17:26

5 Answers 5

3

If I understand your question correctly you don't want to ad an empty div if your query returns exact multiples of 3 items.

So instead this:

if($i % 3 == 0)

Use this:

if($i % 3 == 0 && $wp_query->post_count != $i)

My suggestion was to edit your code to this:

<div class="row">
    <?php 
    $i = 1;
    $wp_query = new WP_Query(array('posts_per_page' => -1, 'post_type' => 'projects'));
    echo '<div class="panel">';
    if($wp_query->have_posts()) : while ($wp_query->have_posts()) :
         $wp_query->the_post();

         the_title();         
         if($i % 3 == 0 && $wp_query->post_count != $i){
             echo '</div><div class="panel">';
         }
         $i++; 
     endwhile; endif;
     echo '</div>';    
  ?>

</div>

But since then Max P. made a better answere using my suggestion. The difference between the two code is when no item is given this code gonna create an empty div, but his not.

Sign up to request clarification or add additional context in comments.

3 Comments

now if there is multiple of 3 items then it will give an extra div, otherwise not!!!! ???
@SalihK As you can see the operator I use (!=) means "is not equal" which means this gonna return false if your $i variable is equal with the number of your items
can you please show how to use your logic with the entire code as above, because it's not working for me. It is more readable and feels a wp way
1

Try this code

<div class="row">
    <?php 
      $i = 1;
      $wp_query = new WP_Query( array( 'posts_per_page' => -1, 
                  'post_type' => 'projects' ) );

      if ( $wp_query->have_posts() ) : 
         while ( $wp_query->have_posts() ) :
            if($i % 3 == 1) { echo '<div class="panel">'; }
            $wp_query->the_post();
            the_title();         
            if($i % 3 == 0 || $i == $wp_query->post_count) { echo '</div>'; }
            $i++; 
         endwhile;
      endif; 
  ?>

</div>

2 Comments

@SalihK If the item number not divisible by 3 this not gonna add a close div.
@MaxP. You can use or operator $i % 3 == 0 || $i == $wp_query->post_count you gonna get the same result, but I think this would make the code readable.
0

You can add a div after your loop ends if its not a multiple of 3.

<div class="row">
<?php 
    $i = 1;
    $wp_query = new WP_Query( array( 'posts_per_page' => -1, 
        'post_type' => 'projects' ) );
    echo '<div class="panel">';

    if ( $wp_query->have_posts() ) : 
        while ( $wp_query->have_posts() ) :
            $wp_query->the_post();
            the_title();         
            if($i % 3 == 0) { echo '</div><div class="panel">'; }
            $i++; 
        endwhile;
    endif;
    if($i % 3 != 0) { echo '</div><div class="panel">'; }
    echo '</div>';
?>
</div>

Comments

0

There are many ways to solve this problem, here is one:

<div class="row">
<?php 

  echo '<div class="panel">';
  $i = 0;
  $wp_query = new WP_Query(array('posts_per_page' => -1, 
                                 'post_type'      => 'projects'));
  if ($wp_query->have_posts()) { 
    while ($wp_query->have_posts()) {
      if($i % 3 == 0)
      {
        if ($i > 0) echo '</div>';
        echo '<div class="panel">';
      }
      $wp_query->the_post();
      the_title();         
      $i++; 
    }
    echo '</div>';    
  } 

?>
</div>

I also cleaned up the code and got rid of the bug that occurs when there are no posts.

Comments

0

You can add a variable called $j and $j = floor($i/3).

And then if($i % 3 == 0 && $i != 3 * $j) { echo '</div><div class="panel">'; };

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.