To precisely match your three <ul>
pattern you can do this:
$my_query = new WP_Query();
$my_query->query($args);
$skip = 2;
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
echo '<li>'.$post->post_title.'</li>';
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
break;
}
}
echo '</ul>';
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
echo '<li>'.$post->post_title.'</li>';
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
break;
}
}
echo '</ul>';
echo '<ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
echo '<li>'.$post->post_title.'</li>';
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
break;
}
}
echo '</ul>';
However, the following it smaller, neater, and should create any number of <ul>
s depending on the size of the query, which I assume is what you mean when you say "infinite loops this way".
$my_query = new WP_Query();
$my_query->query($args);
$skip = 2;
while ($my_query->have_posts()) {
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
echo '<ul>';
}
$my_query->the_post();
echo '<li>'.$post->post_title.'</li>';
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
echo '</ul>';
}
}
An option using a single <ul>
$my_query = new WP_Query();
$my_query->query($args);
$skip = 2;
echo '<ul>';
while ($my_query->have_posts()) {
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
echo '<li>';
}
$my_query->the_post();
echo '<div>'.$post->post_title.'</div>';
if ( ( ($my_query->current_post-1) % $skip ) == 0) {
echo '</li>';
}
}
echo '</ul>';
The $args
I used to test is...
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 8,
'order' => 'ASC',
);
... but this should work with any that your provide.
It does assume that you don't want to query for a distinct set of posts for each Loop.
Obviously, I did not work this into your existing code but the pattern should be easy to apply.