WordPress怎么获取置顶状态的文章

WordPress怎么获取置顶状态的文章?差点忘了,下面记录下。

查询文章方法有很多,这里使用WP_Query,然后其实主要用到get_option('sticky_posts')。

$args = array(
    'post__in' => get_option('sticky_posts'),
    'ignore_sticky_posts' => 1 // 避免重复显示
);
$sticky_query = new WP_Query($args);
while ($sticky_query->have_posts()) {
    $sticky_query->the_post();
    the_title(); // 输出标题
}
wp_reset_postdata();