我正在尝试输出一个包含三行的循环,如下所示:
<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; ?>
答案 0 :(得分:0)
这个怎么样:你可以用第一个mod号和最后一个玩一下。并确保您总是从$ myposts-&gt; post()
获得3个标题<?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; ?>
答案 1 :(得分: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++;
}
无论如何,你的问题都在于条件......