前言
一般會使用 最新文章 的小工具
但是我想要依照 文章類別 各別顯示最新文章,例如下圖所示
程式碼
參考官方文件,使用 wp_get_recent_posts()
//傳入參數
$args = array(
'numberposts' => 3, //要秀幾筆
'offset' => 0,
'category' => 4, //分類的ID 0:不分類的最新文章
'orderby' => 'post_date', //排序欄位
'order' => 'DESC', //排序方式
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'post_status' => 'publish', //文章狀態
'suppress_filters' => true
);
//取資料
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
//迴圈秀資料
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '">' . $recent["post_title"].'</a> </li> ';
}