仅显示第一个置顶帖子。至少一个帖子必须被指定为“置顶帖子”,否则循环将显示所有帖子:
$sticky = get_option( 'sticky_posts' );
$query = new WP_Query( 'p=' . $sticky[0] );
仅显示第一个置顶帖子,如果没有返回最后发布的帖子:
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts' ),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
仅显示3个置顶帖子,如果没有则不返回任何内容:
$sticky = get_option( 'sticky_posts' );
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
if ( !empty( $sticky ) ) {
// insert here your stuff...
}