php - Why is query_posts causing an infinite loop here?
I have looked around for information explaining my issue but found none. I have a loop which supposedly should list all children categories of 'personnel'. For some reason the loop restarts infinitely. I have checked and it goes out of both the foreach and while loop but still manages to keep looping. Any ideas?
I'm calling the function here:
// Personnel listing
upp_loop('Personal', 'personnel-preview', '<section class="res-table">', '</section>');
And this is the code generating the posts:
// Loop through children (To make headers)
$cats = get_categories('child_of=' . $catID . '&orderby=count&order=DESC');
foreach ($cats as $cat) :
$args = array(
'cat' => $cat->term_id
);
$query = new WP_query($args);
if ($query->have_posts()) :
// Echo the Category name
?> <h2><?php echo $cat->name; ?></h2> <?php
// List all children
while($query->have_posts()) :
$query->the_post();
if (has_post_thumbnail()) {
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
}
else {
$img[0] = get_template_directory_uri() . "/img/program-default.png";
}?>
<a href="<?php the_permalink(); ?>" class="res-td">
<img src="<?php echo $img[0] ?>" alt="<?php the_title(); ?>"/>
<h2><?php the_title() ?></h2>
</a>
<?php
endwhile;
endif;
endforeach;
Answer
Solution:
You need to define your query as variable and then call methods with it.
Make these changes in your code:
query_posts($args)
to$query = query_posts($args)
have_posts()
to$query->have_posts()
the_post()
to$query->the_post()