I am trying to build a content slider so that each slide contains 8 images. To do this I need to add the 'row-fluid' class to every 4 posts and 'slide' class to every 8 posts in my WP query.
HTML of what I am try to achieve -
<div class="coda-slider" id="slider-id">
<div class="slide">
<div class="row-fluid">
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
</div><!-- /row-fluid -->
<div class="row-fluid">
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
</div><!-- /row-fluid -->
</div><!-- /slide -->
<div class="slide">
<div class="row-fluid">
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
</div><!-- /row-fluid -->
<div class="row-fluid">
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
<div class="span3">
<img src="...">
</div>
</div><!-- /row-fluid -->
</div><!-- /slide -->
</div><!-- /coda-slider -->
My query that doesn't work correctly -
<?php
$args = array( 'post_type' => 'video', 'posts_per_page' => 10,);
$the_query = new WP_Query( $args );
echo '<section id="our-clients">';
echo '<div class="coda-slider" id="slider-id">';
$i = 1;
echo '<div class="slide">';
echo '<div class="row-fluid">';
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div class="span3">';
the_post_thumbnail();
echo '</div>';
if($i % 8 == 0) {echo '</div><div class="slide">';}
elseif($i % 4 == 0) {echo '</div><div class="row-fluid">';}
$i++; endwhile; endif;
echo '</div>'; //row-fluid
echo '</div>'; //slide
echo '</div>'; //coda-slider
echo '</section>';
What the query is printing out -
The php is adding the 'slide' class to every 8 posts but the first 'slide' class isn't closing correctly. This probably sounds really confusing, so let me know if you need additional information.
I appreciate the help!
This code is from another answer on SO
How do I add a class to every nth item in a php loop (wordpress)
https://stackoverflow.com/a/12698408/804087
You may try this using get_posts and array_chunck
Try this ;)