大家新年快乐!这是我的代码。有没有办法通过循环来缩短代码?在循环中,第一个字段显示一次,相应的子字段显示5个元素。 例如: 第一个字段:Web浏览器 子字段元素:firefox,chrome,IE,opera,maxthon
<div class="row">
<?php
$sort=get_field('category');
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$sort[ 0 ]['fenlei']."
<a id='browser'></a></h3>
<div class='bor'></div>";
$websites = get_field('classification');
$row_count = count($websites);
for ($i = 0; $i <= 4; $i++){
include( get_stylesheet_directory() . '/template.php');
}
?>
</div>
<!--end row1-->
<div class="row">
<?php
$sort=get_field('category');
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$sort[ 1 ]['fenlei']."
<a id='browser'></a></h3>
<div class='bor'></div>";
$websites = get_field('classification');
$row_count = count($websites);
for ($i = 5; $i <= 9; $i++){
include( get_stylesheet_directory() . '/template.php');
}
?>
</div>
<!--end row2-->
<div class="row">
<?php
$sort=get_field('category');
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$sort[ 2 ]['fenlei']."
<a id='browser'></a></h3>
<div class='bor'></div>";
$websites = get_field('classification');
$row_count = count($websites);
for ($i = 10; $i <= 14; $i++){
include( get_stylesheet_directory() . '/template.php');
}
?>
</div>
<!--end row3-->
template.php的内容
<div class="col-lg-2dot4">
<div class="thumbnail" style="height: 360px;">
<a href="<?php echo $websites[ $i ]['link']; ?>" title=<?php echo $websites[ $i ]['name']; ?> target="_blank">
<img src="<?php echo $websites[ $i ]['icon']; ?>" width="194" height="97" />
</a>
<div class="caption">
<h3>
<a href="<?php echo $websites[ $i ]['link']; ?>" title="Firefox" target="_blank"><?php echo $websites[ $i ]['name']; ?></a>
</h3>
<p><?php echo $websites[ $i ]['description'];; ?></p>
</div>
<ul class="list-inline text-center">
<li style="list-style: none">Download:</li>
<li>
<a href="<?php echo $websites[ $i ]['download']; ?>" target="_blank">32Bit</a>
</li>
</ul>
</div>
</div>
答案 0 :(得分:1)
可能没有意义,但功能是有序的:
function echoCode($sortIndex,$start,$stop) {
$sort=get_field('category');
echo "<div class='bor'></div>
<h3 style='text-align:center'>".$sort[$sortIndex]['fenlei']."
<a id='browser'></a></h3>
<div class='bor'></div>";
$websites = get_field('classification');
$row_count = count($websites);
for ($i = $start; $i <= $stop; $i++)
include( get_stylesheet_directory() . '/template.php');
}
然后用(0,0,4)和(1,5,9)调用两次。
答案 1 :(得分:0)
<?php
function print_row($sort, $websites) {
return function($limit_offset, $i) use($sort, $websites) {
$start = $limit_offset[0];
$end = $limit_offset[0] + $limit_offset[1];
return "<div class='bor'></div>
<h3 style='text-align:center'>"
. $sort[$i]['fenlei']
. "<a id='browser'></a></h3>
<div class='bor'></div>"
. implode('', array_map(range($start, $end), row_template($websites)));
};
}
function row_template($websites) {
return function($i) use($websites) {
ob_start();
include(get_stylesheet_directory() . '/template.php');
return ob_get_contents();
};
}
$sort = get_field('category');
$websites = get_field('classification');
$offsets = [[0, 4], [5, 4], [10, 4]];
$results = array_map($offsets, print_row($sort, $websites));
print implode('', $results);