2

I have seen the answer on Stack Overflow, "Example Query Posts Showing the Latest Post with the Featured Image".

query_posts( 'showposts=1' );
while ( have_posts() ) : the_post();
    ?><h3 class="home link"><a href="<?php the_permalink(); ?>"><?php 
        the_title(); ?></a></h3><?php
    the_post_thumbnail();
    the_excerpt();
endwhile;

But I would like to know how I get latest 5 posts?

1
  • 5
    Please post the code that you got from that link. No one is going to search for it through millions of questions :-) Commented Sep 13, 2015 at 7:31

1 Answer 1

4

Getting most recent post in WordPress have builtin function named wp_get_recent_posts( $args, $output ). So let's try with this function to get most recent 5 post

$args = array(
    'numberposts' => 5,
    'offset' => 0,
    'category' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish',
    'suppress_filters' => true );

OR

$args = array( 'numberposts' => '5' );

$recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $recent ){
        echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .        $recent["post_title"].'</a> </li> ';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    the_post_thumbnail('thumbnail');
}
    }

Here thumbnail, medium, large, full is image size for more Click But Other few way you can get you post details like

1- Loop with get_posts() 2- Loop with query_posts() 3- Loop with WP_Query()

  • To modify the default loop, use query_posts()
  • To modify loops and/or create multiple loops, use WP_Query()
  • To create static, additional loops, use get_posts()

But remember this They do different things, so it depends on the situation: wp_reset_query destroys the previous query and sets up a new query using wp_reset_postdata. Best used with query_posts to reset things after a custom query.

wp_reset_postdata restores global $post variable to the current post in the default query. Best used with WP_Query.

But Best way to customize query post is WP_Query()

<ul>
        // Define our WP Query Parameters
        <?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>`

        // Start our WP Query
        <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

            // Display the Post Title with Hyperlink
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

            // Display the Post Featured Image
            <li><?php
                if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                    the_post_thumbnail('thumbnail');
                } ?></li>

            // Repeat the process and reset once it hits the limit
            <?php
        endwhile;
        wp_reset_postdata();
        ?>
    </ul>

if you want to learn more then go there :Click
and Click

3
  • 1
    Please note, you should never ever use query_posts to alter the main query. It breaks the main query and reruns the query, so it is also much slower. ALWAYS use pre_get_posts to alter the main query Commented Sep 13, 2015 at 16:38
  • @PieterGoosen ya right, i forgot to mention this caution but I mention about Best Practices WP_Query for Multiple Query situation.
    – MD MUSA
    Commented Sep 13, 2015 at 17:49
  • Thanks for your kindly answer bro. I have tried your code with customizing. Finally I have satisfied.
    – Kyaw Thein
    Commented Sep 15, 2015 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.