doudeng8691 2017-08-02 04:13
浏览 1235
已采纳

调整Bootstrap“行”以适应各种#列“col-xs-12 col-sm-4 col-md-3”?

Inserting posts on a page using WP Loop. Trying to achieve a grid layout using Bootstrap columns class "col-md-3" for example. I was able to find useful code but it's missing one part to what I'm trying to achieve. Basically, when you use the WP Loop to pull and display posts on a page you can put the Bootstrap column class "col-md-43" for each posts. That way each post is in a column and you basically create a grid of posts. Looks great, except that each row has different heights and then you have weird spacing/gaps.

OK so there are many solutions out there and I liked the one proposed by Zarko Jovic. https://stackoverflow.com/a/35963572/2243165

Basically the solution is to put the columns inside a "row" class. Problem solved.

This works except I'm not sure how to go about it if I have different columns based on the screen size. For example I use "col-xs-12 col-sm-4 col-md-3". Have no idea if it's possible to create a row that adjusts for each of the 3 different column sizes/classes. The code in the link is written to use 4 columns in a row. But when using "col-xs-12 col-sm-4 col-md-3" class the row sometimes has 1 column, sometimes 3, and other times 4 columns per row :(

Here's my code so far without placing the "row" class.

<?php
// Force loop to display defined custom post type 
$args2 = array(
    'post_type' => 'i',
    'author' => get_queried_object_id(), 'showposts' => 100
    );

$editors_pages = new WP_Query($args2);

// The loop
if ($editors_pages->have_posts()) : while ($editors_pages->have_posts()) : $editors_pages->the_post(); ?> 

<div class='col-xs-12 col-sm-4 col-md-3'>
    <div class='author-pages-title'><h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4></div>
</div>

<?php endwhile; else : ?>
    <p class='align-center'>Sorry, no pages posted yet by this user.</p>
<?php endif;
wp_reset_postdata();

?>
  • 写回答

2条回答 默认 最新

  • dou7851 2017-08-02 05:09
    关注

    A good way to tackle this is to give min height to your columns. This You may need a media query for each size i.e. xs,md.

    // The loop
    
    <div class="row"> // put a div 
    if ($editors_pages->have_posts()) : while ($editors_pages->have_posts()) : $editors_pages->the_post(); ?> 
    
    <div class='special-min-height col-xs-12 col-sm-4 col-md-3'> // add a class to reference too.
        <div class='author-pages-title'><h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4></div>
    </div>
    
    <?php endwhile; else : ?>
        <p class='align-center'>Sorry, no pages posted yet by this user.</p>
    <?php endif;
    
    </div>
    

    CSS

    .row .special-min-height {
        min-height: 200px;
    }
    

    Then you can write media query for the remaining screens giving different min height.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来