要取得 WordPress 置頂文章可以透過 WP_Query() 丟進去相對應的參數值:
$sticky = get_option('sticky_posts');
if ( $stickies ) {
$args = [
'post__in'=> $stickies,
];
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//show data here
}
}
}
這樣即使在同一頁(一般是文章列表的 archive.php)再去使用 have_posts() 還是可以得到原本所有文章的列表。