How to display last 3 posts (recent posts) in a static page?

I usually use this approach:

wrong approach

<?php query_posts( array(
   'category_name' => 'news',
   'posts_per_page' => 3,
)); ?>

<?php if( have_posts() ): while ( have_posts() ) : the_post(); ?>

   <?php the_excerpt(); ?>
   <?php endwhile; ?>

<?php else : ?>

   <p><?php __('No News'); ?></p>

<?php endif; ?>

With the help of @swissspidy the correct way is this one:

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'news',
      'posts_per_page' => 3,
   )); 
?>

<?php if ( $the_query->have_posts() ) : ?>
  <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php the_title(); ?>
    <?php the_excerpt(); ?>

  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>

<?php else : ?>
  <p><?php __('No News'); ?></p>
<?php endif; ?>

See @codex for more info.

Leave a Comment