Hirohito Yamada
Hirohito Yamada

Reputation: 387

Latest blog post in wordpress

I wrote this code below in functions.php in order to create a shortcode to how the LATEST blog post.

function wptuts_recentpost($atts, $content=null){
$getpost = get_posts( array('number' => 1) );
$getpost = $getpost[0];
$return = $getpost->post_title . "<br />" . $getpost->post_excerpt . "…";
$return .= "<br /><a href='" . get_permalink($getpost->ID) . "'>
<em>read more →</em></a>";
return $return;
}
add_shortcode('newestpost', 'wptuts_recentpost');

How could I alter this so that I can create shortcode for, 2nd, 3rd and 4th latest blog post?

Upvotes: 1

Views: 64

Answers (2)

user4055330
user4055330

Reputation:

You can also use offset:

$getpost = get_posts( array('number' => 1, 'offset' => 1) );

Upvotes: 2

Hariharan
Hariharan

Reputation: 1204

Use wp_get_recent_posts function as mentioned in below link

https://developer.wordpress.org/reference/functions/wp_get_recent_posts/#source

Upvotes: 0

Related Questions