1

I have an array and I'm getting the results as list elements. -- What I'm trying to do though is to count the total number of items and have it split so that one list would appear on the left (via CSS of course) and the other half on the right.

My code thus far is below...

<?php 
$terms = get_field('featured_cities');
foreach ($terms as $term) {
  echo '<li><a href="'.get_term_link($term->slug, 'cities').'">'.$term->name.'</a></li>';
}
?>

Is it possible someone could point me in the direction of getting this done?

4

2 回答 2

1

$terms包含您的全部元素集。将堆栈平均拆分然后打印两个列表是一件简单的事情。

这是一个很好的方法:

 $list = []; $list2 = [];
 foreach ($terms as $k => $term) {
    if ( ($k % 2) == 0) $list[] = $term;
    else $list2[] = $term;
 }
 // You now have two lists, $list and $list2.

如果您希望仅在一个循环中执行此操作,请考虑设置float: left; width: 50%li元素的样式,但如果您沿着这条路线走,您将需要一个元素,clear: both以防止令人讨厌的样式意外。

于 2013-05-28T02:28:20.357 回答
1

您可以使用多列布局来分隔两列

于 2013-05-28T02:30:26.930 回答