Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLoren Shumaker-Chupp
2,711 PointsInfinite Loop
I keep getting an infinite loop when I add in the WP_Query to my page-projecttype.php file. I think I did everything the same way as is posted in the video, and I redid the page from scratch but came up with the same issue. Any clue why this would be happening?
page-projecttype.php
<div class="container">
<?php
$args = array(
'post_type' => 'project'
);
$query = new WP_Query( $args );
?>
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post; ?>
<p>test</p> <!-- THIS IS LOOPING INFINITELY -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
1 Answer
Liam Maclachlan
22,805 PointsJust something worth trying man: Doesn't 'the_post' need an opening and closing bracket?
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
Not sure if this will solve the problem but I have a feeling it might help :)
It is most likely because there are no posts for the if statement if there is no post_type so it returns false. When it is instantiated with the post_type, it hits the loop and will not, I'm guessing, increment the loop in relation to the post.
Liam Maclachlan
22,805 PointsJust tried it on my site, that should sort it :)
Loren Shumaker-Chupp
2,711 PointsYeah I missed it, thanks!
Liam Maclachlan
22,805 PointsHaha. No problem man. Glad I could help :)
Loren Shumaker-Chupp
2,711 PointsLoren Shumaker-Chupp
2,711 PointsThe issue only appears when the slug actually applies to a post type. If I put anything else in the place of
project
that I don't have set as a post type the loop disappears (however the whole loop disappears so there isn't any content at all).