简体   繁体   English

如何使用PHP在3个COLS之后在Bootstrap 3中创建新的ROW?

[英]How to create new ROW in Bootstrap 3 after 3 COLS with PHP?

I really need some help here. 我真的需要一些帮助。 I am converting my website from plain HTML to WordPress. 我正在将我的网站从纯HTML转换为WordPress。 However i noticed a big problem, on the static website i have a ROW with 3 cols which works just fine. 但是我注意到一个大问题,在静态网站上我有一个带有3个列的ROW,它的工作原理很好。 In Wordpress i noticed that the ROW doesn't make a new ROW just continue to add the new col next to the 3rd col. 在Wordpress中,我注意到ROW并没有创建新的ROW,只是继续在第3个col旁边添加新的col。

How can i fix this with a loop or something? 我该如何用循环之类的东西解决这个问题?

My code: 我的代码:

           <?php $query = new WP_Query(array('post_type' => 'wpa_diensten',  'posts_per_page' => -1));  ?>

            <?php if($query->have_posts()): global $more;  ?>
            <?php while($query->have_posts()): $query->the_post(); $more = 0; ?>

            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 col-lg-height col-md-height col-sm-height col-top <?php post_class( 'clearfix' ); ?>">
                        <span class="wpa-service-bim-image"></span>
                            <h1><?php the_title(); ?></h1>

                            <?php if ( has_post_thumbnail($post->ID) ): ?>
                                <p><?php echo get_the_post_thumbnail( $post->ID, 'large', array('class' => "img-responsive")); ?></p>
                            <?php endif; ?>                               

                            <?php the_content(); ?>

                            <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Lees verder >></a></p>
            </div><!-- end: col -->

            <?php endwhile; ?>
        <?php endif; ?>
        </div>
    </div>
</div>

add a $count variable outside your while loop with initial value of zero 在您的while循环外添加一个初始值为零的$ count变量

then inside your while loop after each loop increase the $count with 1 然后在每个循环之后的while循环内将$ count加1

add if condition $count%3==0 如果条件$ count%3 == 0则添加

if true make new row 如果为true,则创建新行

else make new col 否则做新的col

$count=0
while($query->have_posts()): $query->the_post(); $more = 0;
$count+=1;
if($count%3==0){
//make new row
}
else{
//make new col
}

You can also use the bootstrap responsive column resets instead of creating new rows for each 3 columns. 您也可以使用引导响应列重置,而不是为每3列创建新行。 Then you run into trouble with the 2 columns for xs (col-xs-6). 然后,您在使用xs的2列(col-xs-6)时遇到了麻烦。

Check out the bootstrap documentation: http://getbootstrap.com/css/#grid-responsive-resets 请查看引导程序文档: http : //getbootstrap.com/css/#grid-sensitive-resets

In your case you can add a reset for sm after each 2 columns and a reset after each 3 columns for md and lg. 在您的情况下,可以在每2列之后添加一次sm重置,并在每3列之后添加md和lg重置。

See the example below: 请参阅以下示例:

<?php $query = new WP_Query(
    array(
        'post_type' => 'wpa_diensten',  
        'posts_per_page' => -1
    )
); ?>

<?php $i = 1; // set a counter before the loop ?>

<?php if($query->have_posts()): global $more;  ?>

    <div class="row">

    <?php while($query->have_posts()): $query->the_post(); $more = 0; ?>

        <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 col-lg-height col-md-height col-sm-height col-top <?php post_class( 'clearfix' ); ?>">
            <span class="wpa-service-bim-image"></span>
            <h1><?php the_title(); ?></h1>

            <?php if ( has_post_thumbnail($post->ID) ): ?>
                <p><?php echo get_the_post_thumbnail( $post->ID, 'large', array('class' => "img-responsive")); ?></p>
            <?php endif; ?>                               

            <?php the_content(); ?>

            <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Lees verder >></a></p>

        </div><!-- end: col -->

        <?php if ( $i%2 == 0 ){ echo '<div class="clearfix visible-sm-block">'; } // show clearfix after each 2 cols for xs ?>
        <?php if ( $i%3 == 0 ){ echo '<div class="clearfix visible-lg-block visible-md-block">'; } // show clearfix after each 3 cols for lg and md ?>

    <?php endwhile; ?>

    </div><!-- end: row -->

<?php endif; ?>

I think this is how your code should look. 我认为这就是您的代码的外观。 You might have to tweak the numbers...I think the maths is right! 您可能需要调整数字...我认为数学是正确的! All this does is echo out the opening and closing div in the appropriate places if the loop we are on is a multiple of 3. 如果我们所在的循环是3的倍数,则所有这些操作都会在适当的位置回显开和关div。

<?php $i = 1; # start a loop counter at 1 ?>
<?php if($query->have_posts()): global $more;  ?>
<?php while($query->have_posts()): $query->the_post(); $more = 0; ?>

<?php if ($i % 3) == 0 : ?><div class="row"><?php endif; # If the number of the loop we are on divided by 0 is 3, we are in a "third" loop and you want a row opening div?>

<div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 col-lg-height col-md-height col-sm-height col-top <?php post_class( 'clearfix' ); ?>">
            <span class="wpa-service-bim-image"></span>
                <h1><?php the_title(); ?></h1>

                <?php if ( has_post_thumbnail($post->ID) ): ?>
                    <p><?php echo get_the_post_thumbnail( $post->ID, 'large', array('class' => "img-responsive")); ?></p>
                <?php endif; ?>                               

                <?php the_content(); ?>

                <p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">Lees verder >></a></p>
</div><!-- end: col -->

<?php if ($i % 3) == 0 : ?><div class="row"><?php endif; # If the number of the loop we are on divided by 0 is 3, we are in a "third" loop and you want a row closing div ?>

<?php $i++;
<?php endwhile; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM