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 trialCarla Thomas
Front End Web Development Techdegree Student 16,039 PointsOne Page Site: loop not working
I am following along with the "One Page Site" tutorial. The following is what I have in the one-page-site.php template which should loop all pages on the Home page but it isn't working.
Please help.
<?php
/**
* Template Name: One Page Site
*
* @package One Page Theme
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC');
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ): while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
1 Answer
Ben Griffith
5,808 PointsLooks like your if statement is not checking the right condition.
Try changing it from;
if ( have_posts() ):
to
if ( $the_query->have_posts() ):
Hope this helps :)
Carla Thomas
Front End Web Development Techdegree Student 16,039 PointsCarla Thomas
Front End Web Development Techdegree Student 16,039 PointsThanks Ben. I made the correction but it didn't work.
Ben Griffith
5,808 PointsBen Griffith
5,808 PointsGiven your original code a go and it works fine for me... so makes me suspect that there's a problem elsewhere. Not sure what to suggest.. sorry!
Edit: Sorry if this is a silly question that may seem obvious, but have you made some pages in wordpress (not posts)? Does it work if you switch the post_type in your query to 'posts' ?