While() creating infinite loop


Happy New Year!

I’m trying to do something like that:

<?php while ( have_posts() ): ?>
<?php endwhile; ?>

And here are my functions:

function have_posts () {
	foreach($posts->all() as $current) {
	return false;

function the_title() {
	$current = $thePost->get();
	echo $current->title;

Can anyone spot my error?


-jj. :slight_smile:

The problem is that each iteration of while() evaluates have_posts() so every time the condition is checked it runs the foreach loop. This would be a simple fix:

<?php foreach ( have_posts()  as $post): ?> 
    <p><?php echo $post->title;?></p> 
<?php endwhile; ?>

function have_posts () { 
    return $posts->all();

Many thanks for the reply :slight_smile:

What about a WP-like approach? Would you know how to do that?

<?php while ( have_posts() ): the_post()): ?>

You’d have to have a parameter to the function, which would fairly degenerate the while into a for.