连续包裹每3列

时间:2015-06-08 18:35:25

标签: php

我希望连续包含3列,然后连续包含3列,依此类推。我的模数运算符是问题还是其他什么?感谢。

<?php if( have_rows('service') ): ?>

            <?php while ( have_rows('service') ) : the_row(); ?>

            <?php if($counter % 3 === 0) :    echo '<div class="row"'; endif; ?>

                        <div class="col grid_4_of_12 price-container team_member">
                            <span class="price"><?php the_sub_field('service_price'); ?></span>
                              <img src="<?php the_sub_field('service_image'); ?>" />
                              <h3 style="font-size:18px;" class="member_name"><?php the_sub_field('service_name'); ?></h3>
                              <p><?php the_sub_field('service_description'); ?></p>
                       </div>
                       <?php $counter++; if($counter % 3 === 0) :  echo '</div>'; endif; ?>
                <?php endwhile; ?>

            <?php else :  ?>
            <?php echo 'No Rows Found'; ?>
        <?php endif; ?>

5 个答案:

答案 0 :(得分:1)

第一个div似乎缺少结束括号。改为......

<?php if($counter % 3 === 0) :    echo '<div class="row">'; endif; ?>

提示:请务必查看调试时生成的HTML源代码。

答案 1 :(得分:1)

writeConcern

答案 2 :(得分:0)

在递增$counter

之前,您需要关闭div
<?php $counter++; if($counter % 3 === 0) :  echo '</div>'; endif; ?>

应该改为

<?php if($counter % 3 === 0) :  echo '</div>'; endif; $counter++;  ?>

答案 3 :(得分:0)

<?php 
    // add a counter
    $counter = 1;
    ?>
      <?php $query = new WP_Query(array("post_type" => "services","posts_per_page" => -1 )); ?>
      <?php if($query->have_posts()):?>
    <div class="row">
      <?php while($query->have_posts()):$query->the_post();?>
      <div class="col-md-4">
        <div class="service-media"> <?php the_post_thumbnail(); ?> </div>
        <div class="service-desc">
          <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
          <?php the_content(); ?>
        </div>
      </div>
      <?php if($counter % 3 === 0) :?>
        </div><div class="row">
      <?php endif; ?>
     <?php $counter++; endwhile; endif; wp_reset_postdata(); ?>

答案 4 :(得分:0)

<?php
    //Wrap every 3 elements with div.row
    $index = 1;

    foreach( $sub_pages as $sub_page ) {

        if ($index % 3 == 1 || $index == 1) { // beginning of the row or first
            echo '<div class="row">';
        }

        echo '<div class="col-sm-4">';

        echo $sub_page->post_title;
        echo $sub_page->post_excerpt;

        echo '</div><!-- .col -->';

        if ($index % 3 == 0 || $index == count($sub_pages)) { // end of the row or last
            echo '</div><!-- .row -->';
        }

        $index++;
    }
    //Wrap every 5 elements using array_chunk():
    $chunks = array_chunk($original, 5);
    foreach ($chunks as $each_chunk) {
         // echo out as an unordered list
     }
    ?>