mgmyer
11/4/2014 - 6:49 PM

Divide multiple posts from WP query into groups (for a slider or something else)

Divide multiple posts from WP query into groups (for a slider or something else)

<?php
				$my_query = new WP_Query();
				// Query args
				$args = array( 'post_type' => 'team', 'posts_per_page' => -1, 'order' => 'ASC' );
				$loop = new WP_Query( $args );
				$divider = 10;
				if( $loop->have_posts() ) : ?>
					<!-- open starting div before loop -->
					<li class="team-members clearfix">
						<?php while( $loop->have_posts() ) : $loop->the_post(); ?>
							<div class="member">
								<img src="<?php the_field('photo'); ?>" alt="" class="member-photo" /> 
								<div class="member-info">
									<h5 class="member-name"><?php the_title(); ?></h5>
									<h5 class="member-title"><?php the_field('position'); ?></h5>
								</div>
							</div>
						<?php $current_position = $loop->current_post + 1; // current_post starts at 0 ?>
						<?php if( $current_position < $loop->found_posts && $current_position % $divider == 0 ) : ?>
						<!-- if position is equal to the divider and not the last result close the currently open div and start another -->
					</li>
					<li class="team-members">

					<?php endif; ?>
					<?php endwhile; ?>
					</li><!-- close whichever div was last open -->

<?php endif; ?>