dtvx3420 2016-03-31 21:11
浏览 25

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

  • 写回答

1条回答 默认 最新

  • dongquweng5152 2016-04-01 12:16
    关注

    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; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序