Infinite loop when nesting have_posts()

Question

I need to create blocks of posts in my theme. So far, I only thought of one possible implementation – a nested loop:

The Code

for ($i = 0; have_posts(); $i++) {
    for ($j = 0; have_posts() && $j < 100; $j++) {
        the_post();
        echo "<p>$i, $j</p>";
    }
}

The Problem

Alas, this code generates an endless loop. have_posts() returns true in the loop on the outside every time.

What I tried

Using the_content() in the outside loop. It doesn’t solve the problem and makes the inside loop iterate one time less. THat’s the only thing I thought of.

0
, Coredem 3 years 2020-04-07T12:51:47-05:00 0 Answers 85 views 0

Leave an answer

Browse
Browse