you can use the build-in lop counter to determine the output of the li tags;
example:
<?php while( have_posts() ) : the_post();
if( $wp_query->current_post%4 == 0 ) echo '<li>'; ?>
<div>
<?php echo 'post'.($wp_query->current_post+1); //output here// ?>
</div>
<?php if( $wp_query->current_post%4 == 3 || $wp_query->current_post == $wp_query->post_count-1 ) echo '</li>';
endwhile; ?>
worked like a charm
Great help and thank you.
How to implement into this code with li tags?
That code works for posts
<?php /* Time to create a new loop that pulls out image attachments for this post */
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'numberposts' => 999, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order ID', 'order' => 'ASC', );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' );
echo '<div class="centerImg">';
echo '<a rel="prettyPhoto-img" href="';
echo $image_attributes[0];
echo '">';
echo '<img src="';
echo $image_attributes[0];
echo '" />';
echo '</div>';
?>
<?php } ?>
<?php } ?>
you need to run a counter variable in the foreach loop, and check for ‘before position 1,5,9,etc’ and ‘after position 4,8,12,etc,or after last in loop’ – a pure php exercise and nothing particular to WordPress.
example:
<?php /* Time to create a new loop that pulls out image attachments for this post */
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'numberposts' => 999, 'post_status' => null, 'post_parent' => $post->ID, 'orderby' => 'menu_order ID', 'order' => 'ASC', );
$attachments = get_posts( $args );
if ( $attachments ) {
$counter = 0;
echo '<ul>';
foreach ( $attachments as $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' );
if( $counter%4 == 0 ) echo '<li>'; //open <li> before every fourth item
echo '<div class="centerImg">';
echo '<a rel="prettyPhoto-img" href="';
echo $image_attributes[0];
echo '">';
echo '<img src="';
echo $image_attributes[0];
echo '" />';
echo '</a>';
echo '</div>';
if( $counter%4 == 3 || $counter == count( $attachments )-1 ) echo '</li>'; //close </li> after each block of four or after last item
$counter++; ?>
<?php }
echo '</ul>'; ?>
<?php } ?>
Thank you so much! that helped me too 🙂