Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Looping through lists WordPress

  • SOLVED

I have a gallery that displays images in sets of 9, with each image in an <li></li> set. I need to get the loop to end the unordered list after 9 items, start another list, then end that after the 18th item, and so on.

Example:
<!-- First Set -->
<ul>
<li></li> x 9
</ul>

<!-- Second Set -->
<ul>
<li></li> x 9
</ul>

<!-- Third Set -->
<ul>
<li></li> x 9
</ul>


IMPORTANT: There won't always be sets of 9 items. We may have 6, 16 or 26 depending on the client's content. So if there are no more items to loop through, the list has to close.

Answers (5)

2011-08-29

Utkarsh Kukreti answers:

what code are you currently using?


Dave comments:

Here's what someone else tried to put together for me. It works for the first 9 posts, but breaks on anything greater than 9.

$i = 1;
$j = 1;
$numposts = $wp_query->found_posts; ?>

<ul>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li id="thumb-<?php the_ID(); ?>"><a href="#painting-<?php the_ID(); ?>"><?php the_post_thumbnail('gallery-thumb'); ?></a></li>
<?php if ($i == 9) { echo '</ul>'; }
if ($i == 9 && $numposts > 9) { echo '<ul>'; }
if ($j == $numposts && $i != 9) { echo '</ul>'; } ?>
<?php $i++;
$j++;
endwhile; ?>


Utkarsh Kukreti comments:

$i = 1;
$j = 1;
$numposts = $wp_query->found_posts; ?>

<ul>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li id="thumb-<?php the_ID(); ?>"><a href="#painting-<?php the_ID(); ?>"><?php the_post_thumbnail('gallery-thumb'); ?></a></li>
<?php if ($i % 9 == 0) { echo '</ul>'; }
if ($i % 9 == 0 && $numposts > $i) { echo '<ul>'; }
if ($j == $numposts && $i % 9 != 0) { echo '</ul>'; } ?>
<?php $i++;
$j++;
endwhile; ?>


Dave comments:

That works perfectly! Thanks

2011-08-29

Erez S answers:

for($i = 0;$i < HOWMANYIMAGES;$i++){
if($i == 0)echo '<ul>';
elseif($i%9 == 0)echo '</ul><ul>';

echo '<li>';
//Here you display the image info using the $i var
echo '</li>';
}
echo '</ul>';

2011-08-29

Fahad Murtaza answers:

Hi Dave

Are you using default wordpress gallery? Please give existing code if any.


Dave comments:

Hi Fahd, existing code has just been added in my reply to Utkarsh


Fahad Murtaza comments:

Hope this works

$i = 1;

$j = 1;

$numposts = $wp_query->found_posts; ?>



<ul>

<?php while ($loop->have_posts()) : $loop->the_post(); ?>

<li id="thumb-<?php the_ID(); ?>"><a href="#painting-<?php the_ID(); ?>"><?php the_post_thumbnail('gallery-thumb'); ?></a></li>

<?php if ($i % 9==0 || $numposts=$i) { echo '</ul>'; }



<?php $i++;

$j++;


Fahad Murtaza comments:

No need for variable $i but since you are going to insert a new <ul> based on multiple criteria, here is the code that should work well

<?php
$i = 1;
$numposts = $wp_query->found_posts; ?>
<ul>
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<li id="thumb-<?php the_ID(); ?>"><a href="#painting-<?php the_ID(); ?>">
<?php the_post_thumbnail('gallery-thumb'); ?>
</a></li>

<?php
//If number of posts is either 9, 18, 27 and so on, close the ul
if ($i % 9==0) { echo '</ul>'; }
// If $i reaches the total number of posts, just close ul
if ($i==$numposts) { echo '</ul>'; }
// Start a new <ul> only if needed i.e if $i is less than the
// total number of items in gallery as represented by $numposts
if($i % 9==0 && $i<$numposts){ echo '<ul>'; }

$i++;
?>
<?php endwhile; // end of the loop. ?>

2011-08-29

Julio Potier answers:

Hello, test this full code:

<?php
$numposts = $wp_query->found_posts;
while ($loop->have_posts()) : $loop->the_post();
for( $i = 0; $i < $numposts; $i++ ) {
if( $i == 0 ) echo "<ul>\n";
elseif( $i % 9 == 0 ) echo "</ul>\n<ul>\n";
?><li id="thumb-<?php the_ID(); ?>"><a href="#painting-<?php the_ID(); ?>"><?php the_post_thumbnail('gallery-thumb'); ?></a></li><?php
}
echo "</ul>\n";
endwhile;
?>


"\n" are here to got a better source code lisibility.

2011-08-29

Siddharth Goyal answers:

Hi,

here's your looping algorithm:



<?php


$k=1;
echo "<ul>";
while(images in gallery){

if($k%9==0)
echo "<li>image[$k]</li><br></ul><br><ul>";
else echo "<li>image</li>";

$k++;
}


?>