douchao1957 2016-03-02 16:43
浏览 33
已采纳

PHP和Wordpress,如何将帖子分成三个 - 初学者

I looked at a few examples on Stackoverflow concerning dividing posts into three columns. I had some difficulty implementing the examples. Hope someone can help.

I have an FAQ page with questions. When user clicks read more the block of text expand. I want to place the questions in three columns. The order of the questions does not matter. As long as it fills up the rows evenly.

It should look like this:
[question X] [question A] [question Z]
[question H] [question J] [question K]
[question B] [question V]

I have structured the Bootstrap grid in such away, to solve my blank row problem, when someone clicks read more and the text expand.

<?php if ( $wp_query->have_posts() ) : ?>
<div class="row custom-gutter-faq">

 <!--COLUMN ONE-->
       <div class="col-lg-4">
         <div class="col-lg-12">
                <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                  <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 endwhile; ?>
             </div>
             </div>
            <!--end one-->

            <!--COLUMN TWO-->
           <div class="col-lg-4">
              <div class="col-lg-12">
                <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                  <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 endwhile; ?>
             </div>
            </div>
            <!--end two-->

            <!--COLUMN THREE-->
            <div class="col-lg-4">
              <div class="col-lg-12">
                <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                  <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 endwhile; ?>
             </div>
           </div>
           <!--end three-->

At the moment i am getting the post three times. I just want to get the post once and divide that post into three columns.

Everything that goes into the first column, needs to go into column one in the code and the same goes for everything that needs to go into the second column needs to go into column two in the code ect..

I have been struggling with this. Could not get the examples on Stackoverflow to work for me.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • duanqian1888 2016-03-02 18:30
    关注

    I am pretty sure this is what you are looking for. An easy way to pull this off is by adding a "counter" variable and add to the count each time a new post is added ex($counter++). Then lastly we see if the "counter" / "number of rows" has a remainder of zero.

    <?php if ( $wp_query->have_posts() ) : ?>
            <?php 
                $counter=0;
                $total_posts = $wp_query->post_count;
                $posts_per_column = ceil($total_posts / 3);
            ?>
          <div class="row custom-gutter-faq">
            <div class="col-lg-4">
              <div class="col-lg-12">
                    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); $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> 
                    <!-- Close and open div if the "counter" divided by the "posts per column" of columns you want equals zero -->
                    <?php if($counter % $posts_per_column == 0) echo '</div></div><div class="col-lg-4"><div class="col-lg-12">'; ?>
                    <?php endwhile; ?>
                </div>
            </div>
          </div>
        <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)