以下函数有两个参数。 numberRestriction是每行允许的项数(4) 儿童是所有类别的对象。 我在计算儿童对象中存在多少个类别 并查明是否有遗留物。
function calculateDivision($numberRestriction, $childrens)
{
$arrayobj = new ArrayObject($childrens);
$itemLenght = $arrayobj->count();
$itemBasket = $itemLenght;
$remaining = $itemBasket % $numberRestriction;
return $remaining;
}
<?php
//$term = get_queried_object();
$childrens = get_categories(array('parent' => 27,'hide_empty' => false));
$rowRestriction =4;
$indeBox = calculateDivision($rowRestriction, $childrens);
$arrayobj = new ArrayObject($childrens);
$itemLenght = $arrayobj->count();
$i = 0;
if($indeBox == 0)
{
foreach($childrens as $index => $children)
{
$cat_image = z_taxonomy_image_url($children->term_id);
$cat_id = $children->term_id;
$desc = @explode('|', $term->description);
?>
<a href="<?php echo get_category_link($cat_id) ?>" >
<div class="food_list_single menus_list" style="background: url(<?php echo $cat_image ?>) no-repeat scroll right center / cover ">
<div class="food_hover menus_hover ">
<p style="width: 100%; overflow: hidden; padding: 10px; text-align: center;" class="food_hover_title_small"><?php echo $desc[2] ?></p>
<p class="food_hover_title"><?php echo $children->name ?></p>
<button class="btn btn-default food_button"><i class="fa fa-eye"></i>View Details</button>
</div>
</div>
</a>
<?php
} //foreach ends
}else
{
//loop all items
//set a counter
//everytime the counter reaches maximum item allowed in a row, counter should reset and add class to row even
//once it finds odd number in a row it should call the row uneven
$counter1 = 0;
foreach($childrens as $index => $children)
{
$counter1++;
$cat_image = z_taxonomy_image_url($children->term_id);
$cat_id = $children->term_id;
$desc = @explode('|', $term->description);
//out put div
?>
<a href="<?php echo get_category_link($cat_id) ?>" >
<div class="food_list_single menus_list" style="background: url(<?php echo $cat_image ?>) no-repeat scroll right center / cover ">
<div class="food_hover menus_hover ">
<p style="width: 100%; overflow: hidden; padding: 10px; text-align: center;" class="food_hover_title_small"><?php echo $desc[2] ?></p>
<p class="food_hover_title"><?php echo $children->name ?></p>
<button class="btn btn-default food_button"><i class="fa fa-eye"></i>View Details</button>
</div>
</div>
</a>
<?php
//also track the last items index
$last_items_index = $index;
?>
<?php
}
echo "Inde items :".$indeBox; //finds how many indeboxes
echo " Last items index " . $last_items_index ;
}
?>
答案 0 :(得分:0)
if($indeBox == 1)
{
//if there are one independent box remaining
//find the last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}
else if($indeBox == 2)
{
//if there are two independent boxes remaining
//find the second last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}
else if($indeBox == 3)
{
//if there are three independent box remaining
//find the third last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}
因为我已经知道连续4个盒子,所以只能有1,2或3个独立的盒子。如果有4个盒子,则没有独立的盒子。我可以运行条件
var newString = Regex.Replace("A B C",@"\s+"," ")