【问题标题】:3 element per row in bootstrap/Wordpressbootstrap/Wordpress 中每行 3 个元素
【发布时间】:2015-11-12 22:57:48
【问题描述】:

我有以下问题:我需要创建非常简单的布局,在每一行我希望有 3 个大小相同的盒子,如果我理解正确,为了实现这一点,我需要构建一个结构像下面这样:

<div class="row">
   <div class=" news col-md-3 col-centered">
   </div>
   <div class=" news col-md-3 col-centered">
   </div>
   <div class=" news col-md-3 col-centered">
   </div>
</div>

这是我在 index.php 中的 php 脚本:

    <?php while(have_posts()) : the_post();?>
       <div class="row">
         <div class=" news col-md-3 col-centered">
          <h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
          <p><?php the_excerpt(); ?> </p>
         </div>
       </div>
    <?php endwhile; wp_reset_query(); ?>

使用此代码,每个框都会获得一个&lt;div class="row"&gt; 元素,如下所示:

  <div class="row">
    <div class=" news col-md-3 col-centered">
    </div>
  </div>
...

我该如何解决这个问题?

这就是我现在得到的:如果一个盒子的高度比另一个盒子高,它会在它下面留下没有任何元素的空间。

盒子的高度取决于内容。我想要的是这样的:

【问题讨论】:

    标签: php html wordpress twitter-bootstrap


    【解决方案1】:

    只需将 row 移到 The Loop 之外,以免重复:

    <div class="row">
        <?php while(have_posts()) : the_post(); ?>
            <div class="news col-md-4 col-centered">
                <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                <p><?php the_excerpt(); ?> </p>
            </div>
        <?php endwhile; wp_reset_query(); ?>
    </div>
    

    此外,您需要将列宽更改为 col-md-4,因为 Bootstrap 使用 12 列网格,并且您希望每行 3 列。

    如果您需要在显示 3 列后实际关闭每一行,则需要使用计数器:

    <div class="row">
        <?php $i = 1; while(have_posts()) : the_post();?>
            <div class="news col-md-3 col-centered">
                <h4><a href="<?php the_permalink();?>"><?php the_title();?></a></h4>
                <p><?php the_excerpt(); ?> </p>
            </div>
            <?php if ( $i % 3 === 0 ) { echo '</div><div class="row">'; } ?>
        <?php $i++; endwhile; wp_reset_query(); ?>
    </div>
    

    最后一个示例将确保清除所有浮点数,并且每行元素都在自己的行上。

    【讨论】:

    • 如果我将 &lt;div class="row"&gt; 放在函数之外并且我有很多帖子,它将创建一个结构,其中包含 row 类中的所有帖子,而不仅仅是每行 3 个帖子。
    • 在 Bootstrap .row 中拥有超过 3 个“列”并没有错。如果您将宽度设置为col-md-4,则额外的列将折叠到页面上的另一个“行”。
    • 你是对的。事实是每个盒子都有不同的高度,我想在同一条线上有 3 个盒子,即使一个比另一个高,我会截取我的实际结果和我的实际结果想拥有
    • 您需要为此使用计数器。我已经更新了我的答案,以展示您将如何做到这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2016-02-10
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多