How to split the loop every 2 and 5 results?
im trying to display my WP loop with this layout:
(POST 1) (POST 2)/
(POST 3) (POST 4) (POST 5)/
(POST 6) (POST 7)/
(POST 8) (POST 9) (POST 10)
and so goes on. Any ideas? Thank you very much.
im trying to display my WP loop with this layout:
(POST 1) (POST 2)/
(POST 3) (POST 4) (POST 5)/
(POST 6) (POST 7)/
(POST 8) (POST 9) (POST 10)
and so goes on. Any ideas? Thank you very much.
With the help of a friend, i got the solution:
<?php
$line = 1;
$col = 1;
while(have_posts()) : the_post();
if ($line % 2 == 0) {
$class = 'grid_2'; // class for the 3 smaller posts
} else {
$class = 'grid_3'; // class for the 2 bigger posts
}
?>
<div class="<?php echo $class; ?>">
<a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php
if (($col == 2 ) || ($col == 5 )) {
$line++;
if ($col == 5) {
$col = 0;
}
}
$col++;
endwhile;
?>
Yes you can do that using $wp_query->current_post inside the loop. It returns the current posts index number inside the loop (starting form 0). Have a look at the following code block
<?php
global $wp_query;
while(have_posts()){
the_post();
the_title();
//do your other stuff
if($wp_query->current_post==1){
//do what you want to do after 2nd post
}else if($wp_query->current_post==5){
//do what you want to do after 6th post
}
}
Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.