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 trial

WordPress

Carla Thomas
seal-mask
.a{fill-rule:evenodd;}techdegree
Carla Thomas
Front End Web Development Techdegree Student 16,039 Points

One 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
Ben Griffith
5,808 Points

Looks 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 :)

Ben Griffith
Ben Griffith
5,808 Points

Given 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' ?