我如何每两个循环加一次UL


How do I add UL LI every two loops on while

我想添加我的ul &li每两个循环…示例

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
 <ul>
    <li> <?php the_title() ?> - <?php the_content() ?></li>
 </ul> 
<?php endwhile; ?>

假设我有4篇文章,我希望结果是这样的

<ul>
 <li>Title 1 - content 1</li>
 <li>Title 2 - content 2</li>
</ul>
<ul>
 <li>Title 3 - content 3</li>
 <li>Title 4 - content 4</li>
</ul>

添加一个计数器变量(start =0),该变量在每次循环结束时递增。然后在每次通过的开始,测试if($counter%2==0){ echo "</ul><ul>";}并将第一个<ul>和最后一个</ul>放在循环之外

我会这样做:

for($i = 0; $i < $numberOfUls; $i++)
{
    $result = '<ul>';
    for($j = 0; $j < $numberOfLis; $j++)
    {
        $result .= '<li>Title content</li>'; // Perhaps an array with the whole list $listContent[$i][$j];
    }
    $result .= '</ul>';
}
echo $result;