0

我正在尝试输出一个由一行包围的三列的循环,如下所示:

<div class="row">
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
</div>
<div class="row">
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
</div>
<div class="row">
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
   <div class="col-sm-4><a href="#">Title</a></div>
</div>

这就是我想出的。这适用于第一行和第二行,但第三行最终在第二行内。任何想法我做错了什么?

<?php 

$i = 0;

while ($myposts->have_posts()) : $myposts->the_post(); ?>

<?php if( $count%3 == 0 ) { echo '<div class="row">'; }; $count++; ?>

<div class="col-sm-4"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>

<?php if( $count%3 == 3 ) { echo '</div>'; }; ?>

<?php endwhile; ?>
4

2 回答 2

0

What about this: You can play around a bit with first mod number and last one. And make sure you always get 3 titles from $myposts->post()

<?php 

$count = 0;
while ($myposts->have_posts()) : $myposts->the_post(); ?>
<?php if( $count%2 == 0 || $count == 0 ) { echo '<div class="row">'; }; ?>
<div class="col-sm-4"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
<?php if( $count%2 == 3 ) { echo '</div>'; };
$count++; ?>
<?php endwhile; ?>
于 2013-11-08T15:08:58.153 回答
0

如果我得到你,你想要每三个记录或类似的东西有一个 div 类行。我怎么不知道输入数据的格式我把这些例子作为参考:

$data = array(
    array('title' => "TITLE1", 'link' => "LINK1"),
    array('title' => "TITLE2", 'link' => "LINK2"),
    array('title' => "TITLE3", 'link' => "LINK3"),
    array('title' => "TITLE4", 'link' => "LINK4"),
    array('title' => "TITLE5", 'link' => "LINK5"),
    array('title' => "TITLE6", 'link' => "LINK6"),
    array('title' => "TITLE7", 'link' => "LINK7"),
    array('title' => "TITLE8", 'link' => "LINK8"),
    array('title' => "TITLE9", 'link' => "LINK9"),
    array('title' => "TITLE10", 'link' => "LINK10"),
); 

$rowCounter = 1;
$dataCounter = 0;
$newRow = true;

while($dataCounter < count($data))
{
    if($newRow == true)
    {
        echo '<div class="row">'."\n";
        $newRow = false;
    }

    echo "\t".'<div class="col-sm-4"><a href="'.$data[$dataCounter]['link'].'">'.$data[$dataCounter]['title'].'</a></div>'."\n";

    if(($rowCounter / 3) == 1 OR ($rowCounter % 3) == 0 OR (count($data) - $dataCounter) < 3)
    {
        echo '</div>'."\n";
        $newRow = true;
    }
    $rowCounter++;
    $dataCounter++;
}

无论如何,你的问题在于条件......

于 2013-11-08T15:30:39.900 回答