【发布时间】:2014-08-15 13:53:48
【问题描述】:
我正在寻找一个很好的解决方案来始终显示一定数量的元素 - 在本例中为 3 - 即使 while 循环测试表达式为假。
这是为了得到一个统一的网格布局,这样我就不需要搞乱等高的列等等......
代码
<ul class="event__lineup">
<?php
$l = 0;
while ( has_sub_field('event_lineup') ) :
echo '<li class="title" itemprop="performer" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">' . get_sub_field('event_artist') . '</span></li>';
if ( $l == 2 ) : break; endif;
$l++;
endwhile;
?>
</ul>
如果说只有 1 位或 2 位艺术家,我希望使用占位符:
echo '<li class="title"> </li>';
我知道 while 循环可能不是最好的解决方案,但是,我不确定如何处理不同的解决方案。
编辑
<ul class="event__lineup">
<?php
for ($i = 0; $i < 3; $i++) {
if ( has_sub_field('event_lineup') ) {
echo '<li class="title" itemprop="performer" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">' . get_sub_field('event_artist') . '</span></li>';
} else {
echo '<li class="title"> </li>';
}
}
?>
</ul>
【问题讨论】: