0

All right guys, I need a little help for a loop. I have a query that get's all images for my database, and I need to separate them in blocks of 6. Every 6 images, one block with div. Somethign like:

<div class='item'>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
</div>


<div class='item'>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
  <li><img></li>
</div>

My code is for the Wordpress Pods Plugin:

<div class="carousel-inner">
<?php 
    $cat = new Pod("galeria"); //nome do pod
    $records = $cat->findRecords("t.ID"); //listado pelo titulo e limite dos pods
?>
<?php if($records%6 == 0): echo $records;?>
    <div class="item">
<?php elseif($records == 6): ?>
    <div class="item active">     
<?php endif; ?>
    <div class="row">
        <?php 
            while ($cat->fetchRecord()) :
                $id = $cat->get_field("ID"); //pega id
                $imagens = $cat->get_field("imagens"); //lista de imagens
                $thumb_url = wp_get_attachment_url( get_post_thumbnail_id( $id ) );
                $permalink = $cat->field('permalink');
        ?>
            <div class="col-sm-2"><a href="<?php echo $permalink; ?>" class="thumbnail"><img src="<?php echo $thumb_url; ?>" alt="Image" class="img-responsive"></a></div>  
        <?php endwhile; ?>
    </div><!--/row-->
<?php if($records%6 == 0): ?>
    </div><!--/item-->
<?php endif; ?>


UPDATE 22/07 - 03:45

Used the code you provide and it's awesome. If someone needs it, this is my final code:

 <div class="carousel-inner">
<?php 
    $cat = new Pod("galeria"); //the Pod name
    $records = $cat->findRecords("t.ID"); //listing items by ID
    $i = 0;
?>
<?php 
    while ($cat->fetchRecord()) : 
        $id = $cat->get_field("ID"); //get id
        $imagens = $cat->get_field("imagens"); //list images
        $thumb_url = wp_get_attachment_url( get_post_thumbnail_id( $id ) ); // the thumbnail url
        $permalink = $cat->field('permalink'); // the pods permalink
?>

<?php if($i % 6 == 0): ?> // every 6 images
        <div class="item <?php if( $i == 0 ){ echo 'active'; //if is the very first, add class 'active' } ?>">
            <div class="row">
<?php endif; ?>
<?php $i++; ?> // increment the $i
                <div class="col-sm-2"><a href="<?php echo $permalink; ?>" class="thumbnail"><img src="<?php echo $thumb_url; ?>" alt="Image" class="img-responsive"></a></div>  // the <li> item

<?php if ( $i != 0 && $i % 6 == 0): ?> // close the .item div every 6 images
            </div>  <!--/row-->
        </div><!--/item-->
<?php endif; ?>

<?php  endwhile; ?>

<?php if ( $i % 6 != 0 ): ?>
        </div><!--/row-->
    </div><!--/item-->
<?php endif; ?>

4

1 回答 1

1

您需要迭代一个变量以与您的模数 6 进行比较以确定要打印的内容。在每个循环上递增它并检查值以确定要输出的容器,从 0 开始。

在循环之外,您还需要检查是否$i % 6 != 0也可以关闭不完整的最终集。完整的模拟代码如下所示:

<?php $i = 0 ?>
<?php while ( $cat->fetchRecord() ) : $i++; ?>
    <!-- open on every sixth record including the first -->
    <?php if ( $i % 6 == 0 ): ?>
        <div class="item">  
    <?php endif; ?>

        <!-- your items go here -->
        <?php 
        $id = $cat->get_field("ID"); //pega id
        echo $id;
        ?>

    <!-- close every sixth result, skipping the first -->
    <?php if ( $i != 0 && $i % 6 == 0 ): ?>
        </div><!--/item-->
    <?php endif; ?>
<?php endwhile; ?>

<!-- close in case of incomplete final set -->
<?php if ( $i % 6 != 0 ): ?>
    </div><!--/item-->
<?php endif; ?>
于 2014-07-22T04:33:41.073 回答