出于某种原因,在这个For each循环中,将每2个div包装成一行有点噩梦。。。。
有人能解释一下吗?
<?php
$terms = get_terms("aircraft_category"); // get all categories
$count = count($terms); //Count how many terms we have
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<div class=\'col-md-6 category\'>";
echo "<h3>";
echo $term->name;
echo "</h3>";
echo $term->description;
echo "<div class=\'link\'>";
echo "<a href=".get_term_link($term).">" . \'See More\' . "</a>";
echo "</div>";
echo " </div>\\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
我尝试添加计数器,但没有成功。。。
任何帮助都会很好。
提前谢谢。
最合适的回答,由SO网友:Gareth Gillman 整理而成
您需要创建一个带有count的变量,然后对照它进行检查,例如。
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo \'<div class="something">\';
}
// other stuff
if($num %2) {
echo \'</div>\';
}
$num++
}
%2是您需要的部分,它检查数字是否可以除以2