Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Theme Development The WordPress Loop Adding the Loop to the index.php File

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,956 Points

Wordpress Theme Development: Only the second post shows up on

Hi all. Something a little unexpected happened while doing this video.

I added a second post to my local development site for Wordpress and the video shows that both posts should show up as they're both picked up by The Loop.

But on mine only the second one shows up. My page is set up so the default template should be all posts.

Here's my code for index.php

<?php get_header(); ?>

<section class="row">
      <div class="small-12 columns text-center">
        <div class="leader">

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); //start the loop ?>
            <h1><?php the_title(); ?></h1>
            <p><?php the_content(); ?></p>   

        <?php endwhile; else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; //endif ?>           
        </div>
      </div>
    </section>

<?php get_footer(); ?>

Hi Jonathan,

It's hard to assess the problem from a distance based on your description and you possibly already tried this, but are you sure that index.php is the file that is displaying the page where you want your posts to show up? Try adding a test somewhere and see if it shows up, and check to see if your theme folder holds a file named home.php. I would also check to see if the post displays correctly on other pages and as a single post.

Otherwise, adding the WP_Query statement like below might help. However, that might not explain why your current code doesn't work.

Hope this helps.

<?php get_header(); ?>

<section class="row">
      <div class="small-12 columns text-center">

        <div class="leader">
            <?php 
               $args = array(
                 'post_type' => 'post',
                 'posts_per_page' => '2'
                );
              $the_query = new WP_Query( $args );
            ?>

        <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <p><?php the_content(); ?></p>   

        <?php endwhile; else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; //endif ?>           
        </div>
      </div>
    </section>
<?php get_footer(); ?>
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,956 Points

Hi Robin,

Thanks for your response.

To be honest I've moved onto the next few videos after this and left it as it is :)

There are now 2 templates.

index.php which is the default template, and page.php along with page-left-sidebar.php which serves as the second Template. This is working perfectly but when I last looked index.php was still only showing up the one post.

My guess is a setting when I was setting up the database is causing the difference between the videos because I followed it to the latter. Or even simply that it might just be a difference between the versions of WordPress.

2 Answers

Hi Jonathan,

I think your comments will be the problem here and they're preventing the PHP tags from closing, e.g.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); //start the loop ?>

Try removing them so the code becomes:

<?php get_header(); ?>

<section class="row">
      <div class="small-12 columns text-center">
        <div class="leader">

        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <p><?php the_content(); ?></p>   

        <?php endwhile; else : ?>
            <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; ?>           
        </div>
      </div>
    </section>

<?php get_footer(); ?>

Does that help?

-Rich

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,956 Points

Blimey would that be it? :-)

I put them in to help me remember what was happening. Wouldn't the comments be ignored by Wordpress like any other file?

I'll let you know of this works.

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,956 Points

Thinking about this further, using multi line comments would do the trick, surely.

What I stupidly thought was that the closing php code block wouldn't be affected by the comment, and I was sure I'd seen an example of similar usage in the video.

Anyway, I will test all of this as I say when I get the chance and let you know, :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,956 Points

Hi Rich,

Unfortunately it looks like this didn't work. I know that both the posts exist so I;m guessing there's some setting somewhere in admin that's causing this not to work.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,956 Points

FIXED :)

The code was absolutely fine. Even down to the comment tags although I'll be careful not to use single line comments where I can.

I went into the Settings page in wp-admin and there inside was the "posts_per_page" setting set to 1.

I changed this and voila. Both the posts now show up.

Thank you both for your responses but I'm going mark this as resolved and give it to Rich for helping me get the ball rolling on the answer,