【问题标题】:Insert/Close row after every 4 in foreach loop在 foreach 循环中每 4 次插入/关闭行
【发布时间】:2014-12-12 15:32:02
【问题描述】:

我知道这应该是简单的数学运算,但我的词汇中不存在这两个词:)

我有一个运行许多画廊图像的循环,我使用引导程序,所以我在循环中的每个项目都有一个 col-md-3 类,我只需要一种方法来添加一个新行并在之后关闭前一个每四个项目。

当我回显我的结束行和开始行时,我尝试添加这个,但它没有起到作用,也许我错过了一些东西,比如基本的数学技能。这是我尝试过的:$i % 4 === 0 && $i !== 0

我几乎做到了,除了循环中的第一项 (0) 单独在一行上,其他的似乎是我想要的 4 到一行。

代码:

if ( $gallery_query->have_posts() ) :

                                $i = 0; //Count

                                //the loop
                                while($gallery_query->have_posts() ) : $gallery_query->the_post(); 

                                ?>

                                <article class="col-md-3 <?php echo $i; ?>" id="post-<?php echo get_the_ID(); ?>" <?php  get_post_class(); ?> >
                                        <div class="masonry-thumbnail border">
                                            <a href="<?php echo get_post_permalink(); ?>" title="<?php echo get_the_title(); ?>" class="inner-shadow"><?php echo get_the_post_thumbnail(get_the_ID(), 'masonry-thumb'); ?></a>
                                        </div><!--.masonry-thumbnail-->

                                    <div class="masonry-details">
                                        <h5><a href="<?php echo get_post_permalink(); ?>" title="<?php echo get_the_title(); ?>"><span class="masonry-post-title"> <?php echo get_the_title(); ?></span></a></h5>
                                        <div class="masonry-post-excerpt">
                                            <?php echo getPostLikeLink(get_the_ID()); ?>  <p class="post-comment"><a href="<?php echo get_post_permalink(); ?>"><i class="fa fa-comments qcomment" title="Comments"></i></a> <?php echo comments_number( ' ', '<sup>1</sup>', '<sup>%</sup>' ); ?></p><?php echo getBookmarkLink(get_the_ID()); ?>
                                        </div><!--.masonry-post-excerpt-->
                                    </div><!--/.masonry-entry-details -->  
                                </article><!--/.masonry-entry-->

                                <?php

                                    if($i % 4 === 0) {
                                        echo '</div><div class="row">';
                                    }

                                    ++$i;

                                endwhile; 

资源: Bootstrap

Similar questions/answers used to get this far

【问题讨论】:

    标签: php twitter-bootstrap foreach


    【解决方案1】:

    想想看:0 mod 4还是0

    你想用 1 开始你的计数器

    1 % 4 = 1
    2 % 4 = 2
    3 % 4 = 3
    4 % 4 = 0 //Close and reopen a row!
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      • 2019-09-04
      相关资源
      最近更新 更多