Getting most recent post in WordPress have builtin function named
wp_get_recent_posts( $args, $output )
. soSo 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' );
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true );
$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 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
1- Loop with get_posts()
2- Loop with query_posts()
3- Loop with WP_Query()
- To modify the default loop, use query_posts()
query_posts()
- To modify loops and/or create multiple loops, use WP_Query()
WP_Query()
- To create static, additional loops, use get_posts()
get_posts()
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' ); ?>
<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
Thanks Musa