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

Gabriel Ward
Gabriel Ward
20,222 Points

Displaying posts on front page from blog

I'm trying to display the latest three posts from my blog on my static front page. I've got a loop going so that my posts display on my static front page, but all of them are displayed, not just the first three. I'm guessing there's something wrong in the WP_Query but not quite sure. Here's my code

<?php
               $latest_blog_posts = new WP_Query(array(
                       'post-per-page' => '3'                   
                    ));
          ?>
                 <?php if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post(); ?>
                    <article class="col-group">
                          <div class ="excerpt">   
                            <h2><?php the_title(); ?></h2>
                            <p class="those"><?php the_excerpt(); ?></p>
                            <a href="<?php the_permalink(); ?>">  <div class ="text"></div></a>
                          </div>       
    </article>
                    <?php endwhile; endif ?>
          <?php
          wp_reset_postdata(); 
          ?>

Thanks for any help

Hi,

I think you should take the quotation marks off the number 3 and it's posts_per_page, not post_per_page. Also, I would first create a variable and pass that through you're WP_Query, like so:

<?php $args = array( 'posts-per-page' => 3,
); $latest_blog_posts = new WP_Query ( $args ); ?>

Hope this helps.

1 Answer

Andres Altuve
Andres Altuve
16,274 Points

<div class="pageMain">

 <?php
           $yourQuery = new WP_Query(array(
                   'post-per-page' : 3               
                ));
           if ( $query->have_posts() ) :
  ?>

<?php 
while ( $yourQuery-> have_posts() ) : $yourQuery->the_post(); 
 ?>
<article class="col-group">
                      <div class ="excerpt">   
                               <h2><?php the_title(); ?></h2>
                                <p class="those"><?php the_excerpt(); ?></p>
                                <a href="<?php the_permalink(); ?>">  <div class ="text"></div></a>
                      </div>                
 </article>
 <?php endwhile; endif; ?>
 <?php   wp_reset_postdata();    ?>

<span class="clear"></span> </div>

<?php get_footer(); ?>