iamafish
iamafish

Reputation: 817

How do I add UL LI every two loops on while

I want to add my ul & li every two loop.. Example

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

Let's say I have 4 posts and I want the result should be like this

<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>

Upvotes: 3

Views: 2251

Answers (2)

span
span

Reputation: 5624

I would do something like this:

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;

Upvotes: 0

Robot Woods
Robot Woods

Reputation: 5687

add a counter variable (start =0) that increments at the end of each pass through the loop. Then at the beginning of each pass, test if($counter%2==0){ echo "</ul><ul>";}and put the first <ul> and last </ul> outside of the loop

Upvotes: 5

Related Questions