PHP将列分为3(数学和额外div问题)

I have a Wordpress site with a FAQ page, consisting of questions. I used Bootstrap as my gird system. I have asked a similiar question before, but now I have a new problem. For more details: PHP and Wordpress - Place content in correct column

The Faq page
enter image description here

The layout should be in 3 columns. It should look like this.

For 3 questions.
[question Z] [question A] [question O]

For 4 questions.
[question Z] [question A] [question O]
[question X]

For 5 questions.
[question Z] [question A] [question O]
[question X] [question V]

What i need.
The column structure should look like this:

//Column1
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="col-lg-12"> question Z </div>
</div>

//Column2
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="col-lg-12"> question A </div>
</div>

//Column3
<div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
<div class="col-lg-12"> question X </div>
</div>

The Problem:
When I have 7 or 11 Faq posts. My code creates an extra div. I would like to remove this div.
Example:

enter image description here


I want my post to be divided into these three columns and no more.
Reason: My faqs expand when you click on them. The aim is to expand the column.

<?php if ( $wp_query->have_posts() ) : ?>
<?php
  $counter=0;
  $columns=3;
  $total_posts = $wp_query->post_count;
  $posts_per_column = ceil($total_posts / 3);
  $posts_per_column_test_value = ($total_posts - 1) / $columns;
  $is_special_case = false;

  if($total_posts != 1 && (intval($posts_per_column_test_value) ==  $posts_per_column_test_value)){
    $is_special_case = true;
    $posts_per_column = $posts_per_column - 1;
}
?>


<div class="container">
    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
            <div class="col-lg-12">
                    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
              if($is_special_case && $wp_query->current_post == 0){
                          $counter++;
                        } else { 
                           $counter++;
                        }
                        ?>
                <div class="faq-all">
                    <div class="faq-item">
                        <h2><?php the_title(); ?></h2>
                        <article>
                            <div class="faq-intro">
                                <?php the_content(); ?>
                            </div>
                            <div class="faq-info">
                                <?php the_content(); ?>
                            </div>
                            <div class="faq-link">
                                <a href="#" class="read-more">LES HELE SVARET</a>
                                <a href="#" class="read-less">LES MINDRE</a>
                            </div>
                        </article>
                    </div>
                </div>
                <?php if($counter % $posts_per_column == 0) echo '</div></div><div class="col-xs-12 col-sm-6 col-md-4 col-lg-4"><div class="col-lg-12">'; ?>
                <?php endwhile; ?>
            </div>
        </div>
    </div>
</div>

Thanks in advance

Try this:

<?php if(have_posts()) : ?>
<?php $no_of_posts = wp_count_posts()->publish; ?>
<div class="row">
   <?php $i=1; while(have_posts()) : the_post(); ?>
       <div class="col-xs-12 col-sm-6 col-md-4 col-lg-4">
         <div class="col-lg-12"> question X </div>
       </div>
       <?php if($i%3==0 && $i!=$no_of_posts) : ?>
       </div>
      <div class="row">
      <?php endif; ?>
   <?php $i++; endwhile; ?>
</div>
<?php endif; ?>