Problem.
What I am trying to do is to call a category specific loop, however, I want whats returned to display from most recent first, to be numerically numbered so that for every 2 that is displayed, will echo a css class ascertained to them and the 3rd result to display a completely different class as this is how I have written my html. Here is what I am trying to get the HTML to display:
<div id="content">
<div class="block1"></div>
<div class="block1"></div>
<div class="block2"></div>
<div class="block1"></div>
<div class="block1"></div>
<div class="block2"></div>
</div>
If there are more results then the first two will be named in the first div and the 3rd of all the results will have that class name assigned to it.
Help would be more than greatly appreciated.
Remarked:
<?php query_posts( 'cat=featured&showposts=4' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php foreach($recent as $index => $postObj) {
$class = $index + 1 % 3 === 0 ? 'block2' : 'block1';
}
?>
<h1><?php the_title(); ?></h1>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php get_footer(); ?>
however its returning the number of posts but under the posts its returning
Warning: Invalid argument supplied for foreach()
Have tried trial and error however, I think that my grammar is atrocious.
What you’re looking for is the modulo operator. What modulo does is find the remainder of a division operation. In effect the result is in a range of 0..N-1, where N % N = 0.
This accomplishes what you want because the loop logic looks like:
Your code needs to be:
The
$index++operator means, "increment $index after this use of it." So, notice how the loop is set up. Before the loop we set$indexto 1. Inside the loop we set$classusing our modulo technique, then increment$index. Then we have to create a container DIV, like you mentioned, and echo the class there.